Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (121)
Showing
with 907 additions and 148 deletions
stages: stages:
- lint
- test - test
test:5.3: &PHP-UNITTESTING cache:
image: tetraweb/php:5.3 paths:
- vendor/
.lint: &lint
image: composer
stage: lint
before_script:
- apk --no-cache add parallel
- php -v
- composer --no-ansi self-update
- composer --no-ansi --version
- composer install --optimize-autoloader --classmap-authoritative --no-interaction --prefer-dist --no-ansi
artifacts:
when: on_failure
paths:
- result
lint:
<<: *lint
script:
- find . -name \*.php ! -path "./vendor/*" | parallel --gnu php -d display_errors=stderr -l {} > /dev/null \;
lint:coding-guideline:
<<: *lint
script:
- mkdir result
- ./vendor/bin/phpcs -s -n --report=full --report-full=result/phpcs-full.txt --report-diff=result/phpcs-diff.txt --report-summary=result/phpcs-summary.txt
lint:php-mass-detection:
<<: *lint
script:
- mkdir result
- ./vendor/bin/phpmd src text phpmd.xml | tee result/phpmd.txt
.test: &test
stage: test stage: test
tags: tags:
- docker - docker
variables: variables:
TIMEZONE: "Europe/Berlin" TIMEZONE: "Europe/Berlin"
WITH_XDEBUG: "true"
before_script: before_script:
- apt-get update; apt-get install -y zip unzip - apt-get update; apt-get install -y unzip
- composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist - composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist
script: script:
- ./vendor/bin/phpunit - ./vendor/bin/phpunit --coverage-text --colors=never
test:5.4: test:5.4:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:5.4 image: tetraweb/php:5.4
test:5.5: test:5.5:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:5.5 image: tetraweb/php:5.5
test:5.6: test:5.6:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:5.6 image: tetraweb/php:5.6
test:7.0: test:7.0:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:7.0 image: tetraweb/php:7.0
test:7.1: test:7.1:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:7.1 image: tetraweb/php:7.1
test:latest: test:latest:
<<: *PHP-UNITTESTING <<: *test
image: tetraweb/php:latest image: tetraweb/php:latest
language: php
cache:
directories:
- $HOME/.composer/cache
php:
- '5.3'
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- '7.1'
install:
- 'composer install --prefer-dist'
script:
- 'php vendor/bin/phpunit -c phpunit.xml.dist'
...@@ -3,24 +3,36 @@ About ...@@ -3,24 +3,36 @@ About
Composer plugin installs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) Standards. Composer plugin installs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) Standards.
[![build status](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/badges/master/build.svg)](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/commits/master) [![Latest Stable Version](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/v/stable)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin) [![Total Downloads](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/downloads)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin) [![License](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/license)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin) [![build status](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/badges/master/build.svg)](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/commits/master) [![coverage report](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/badges/master/coverage.svg)](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/commits/master) [![Latest Stable Version](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/v/stable)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin) [![Total Downloads](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/downloads)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin) [![License](https://poser.pugx.org/higidi/composer-phpcodesniffer-standards-plugin/license)](https://packagist.org/packages/higidi/composer-phpcodesniffer-standards-plugin)
Requirements Requirements
------------ ------------
_TODO_ - `PHP >= 5.4`
- Composer package providing the PHP CodeSniffer Rulesets & Sniffs
Installation Installation
------------ ------------
_TODO_ ```bash
$ composer require higidi/composer-phpcodesniffer-standards-plugin
```
Documentation Change the type of your composer package to `phpcodesniffer-standard` (or optional to `phpcodesniffer-standards`, both will work)
-------------
_TODO_ Your composer.json should looks like:
```json
{
"name": "vendor/my-php-codesniffer-standards",
"description": "My codesniffer standards",
"type": "phpcodesniffer-standard",
"require": {
"higidi/composer-phpcodesniffer-standards-plugin": "^1.0"
}
}
```
Issues Issues
------ ------
Bug reports and feature requests can be submitted on the [Github Issue Tracker](https://github.com/higidi/composer-phpcodesniffer-standards-plugin/issues) Bug reports and feature requests can be submitted on the [Issue Tracker](https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/issues)
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
"role": "Developer" "role": "Developer"
} }
], ],
"support": {
"issues": "https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin/issues",
"source": "https://git.higidi.com/higidi/composer-phpcodesniffer-standards-plugin"
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Higidi\\ComposerPhpCSStandardsPlugin\\": "src/" "Higidi\\ComposerPhpCSStandardsPlugin\\": "src/"
...@@ -24,10 +28,11 @@ ...@@ -24,10 +28,11 @@
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/src/" "Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/"
} }
}, },
"require": { "require": {
"php": "^5.4 || ^7.0",
"composer-plugin-api": "^1.1", "composer-plugin-api": "^1.1",
"squizlabs/php_codesniffer": "^2.0", "squizlabs/php_codesniffer": "^2.0",
"symfony/finder": "^2.1 || ^3.0", "symfony/finder": "^2.1 || ^3.0",
...@@ -35,9 +40,13 @@ ...@@ -35,9 +40,13 @@
}, },
"require-dev": { "require-dev": {
"composer/composer": "^1.4", "composer/composer": "^1.4",
"phpunit/phpunit": "^4.5 || ^5.0.5" "phpunit/phpunit": "^4.8.36 || ^5.0.5",
"phpmd/phpmd": "^2.6"
}, },
"extra": { "extra": {
"class": "Higidi\\ComposerPhpCSStandardsPlugin\\Plugin" "class": "Higidi\\ComposerPhpCSStandardsPlugin\\Plugin",
"branch-alias": {
"dev-master": "1.0.x-dev"
}
} }
} }
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>The coding standard for higidi/composer-phpcodesniffer-standards-plugin.</description>
<arg value="sp"/>
<arg name="colors"/>
<arg name="encoding" value="utf-8" />
<file>./src</file>
<file>./tests</file>
<exclude-pattern>*/Fixtures/*</exclude-pattern>
<rule ref="PSR2"/>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>./tests/bootstrap.php</exclude-pattern>
</rule>
</ruleset>
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<rule ref="rulesets/cleancode.xml" />
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/controversial.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml" />
<rule ref="rulesets/unusedcode.xml" />
</ruleset>
...@@ -11,8 +11,11 @@ ...@@ -11,8 +11,11 @@
bootstrap="tests/bootstrap.php"> bootstrap="tests/bootstrap.php">
<testsuites> <testsuites>
<testsuite name="Project Test Suite"> <testsuite name="Unit Tests">
<directory>./tests/src/</directory> <directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="Integration Tests">
<directory>./tests/Integration/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
......
...@@ -2,6 +2,25 @@ ...@@ -2,6 +2,25 @@
namespace Higidi\ComposerPhpCSStandardsPlugin; namespace Higidi\ComposerPhpCSStandardsPlugin;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Composer\Composer; use Composer\Composer;
use Composer\Installer\BinaryInstaller; use Composer\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller; use Composer\Installer\LibraryInstaller;
...@@ -9,14 +28,19 @@ use Composer\IO\IOInterface; ...@@ -9,14 +28,19 @@ use Composer\IO\IOInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface; use Composer\Repository\InstalledRepositoryInterface;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Symfony\Component\Finder\Finder; use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Standard;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standards;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem; use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standard;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
class Installer extends LibraryInstaller class Installer extends LibraryInstaller
{ {
const TYPE = 'php-codesniffer-standards'; const TYPE = 'phpcodesniffer-standard';
/**
* @var Finder
*/
protected $finder;
/** /**
* Initializes library installer. * Initializes library installer.
...@@ -26,15 +50,19 @@ class Installer extends LibraryInstaller ...@@ -26,15 +50,19 @@ class Installer extends LibraryInstaller
* @param string $type * @param string $type
* @param Filesystem $filesystem * @param Filesystem $filesystem
* @param BinaryInstaller $binaryInstaller * @param BinaryInstaller $binaryInstaller
*
* @SuppressWarnings(PHPMD.ShortVariable)
*/ */
public function __construct( public function __construct(
IOInterface $io, IOInterface $io,
Composer $composer, Composer $composer,
$type = self::TYPE, $type = self::TYPE,
Filesystem $filesystem = null, Filesystem $filesystem = null,
BinaryInstaller $binaryInstaller = null BinaryInstaller $binaryInstaller = null,
Finder $finder = null
) { ) {
parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller); parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller);
$this->finder = $finder ?: new Finder();
} }
/** /**
...@@ -42,23 +70,21 @@ class Installer extends LibraryInstaller ...@@ -42,23 +70,21 @@ class Installer extends LibraryInstaller
*/ */
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
{ {
$isInstalled = parent::isInstalled($repo, $package); if (! parent::isInstalled($repo, $package)) {
if (!$isInstalled) { return false;
return $isInstalled;
} }
$sourceStandards = $this->getSourceStandards($package); $srcStandards = $this->getSourceStandards($package);
$destinationStandards = $this->getDestinationStandards($repo); $dstStandards = $this->getDestinationStandards($repo);
foreach ($sourceStandards as $sourceStandard) { foreach ($srcStandards as $srcStandard) {
if (!$destinationStandards->hasStandard($sourceStandard) if (! $dstStandards->hasStandard($srcStandard)
|| !$this->compareStandards($sourceStandard, $destinationStandards->getStandard($sourceStandard)) || ! $this->compareStandards($srcStandard, $dstStandards->getStandard($srcStandard))
) { ) {
$isInstalled = false; return false;
break;
} }
} }
return $isInstalled; return true;
} }
/** /**
...@@ -79,14 +105,13 @@ class Installer extends LibraryInstaller ...@@ -79,14 +105,13 @@ class Installer extends LibraryInstaller
$this->installStandards($repo, $target, $initial ? false : true); $this->installStandards($repo, $target, $initial ? false : true);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{ {
if (!$repo->hasPackage($package)) { if (! $repo->hasPackage($package)) {
throw new \InvalidArgumentException('Package is not installed: '.$package); throw new \InvalidArgumentException('Package is not installed: ' . $package);
} }
$this->removeStandards($repo, $package); $this->removeStandards($repo, $package);
parent::uninstall($repo, $package); parent::uninstall($repo, $package);
...@@ -96,40 +121,47 @@ class Installer extends LibraryInstaller ...@@ -96,40 +121,47 @@ class Installer extends LibraryInstaller
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
* @param PackageInterface $package * @param PackageInterface $package
* @param bool $override * @param bool $override
*
* @return void * @return void
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/ */
protected function installStandards(InstalledRepositoryInterface $repo, PackageInterface $package, $override = false) protected function installStandards(
{ InstalledRepositoryInterface $repo,
PackageInterface $package,
$override = false
) {
$filesystem = new SymfonyFilesystem(); $filesystem = new SymfonyFilesystem();
$sourceStandards = $this->getSourceStandards($package); $srcStandards = $this->getSourceStandards($package);
$destStandardsBasePath = $this->getPHPCodeSnifferStandardsBasePath($repo); $dstStdBasePath = $this->getPHPCodeSnifferStandardsBasePath($repo);
$this->io->writeError(' Installing PHP-CodeSniffer Standards:', false); $this->io->writeError(' Installing PHP-CodeSniffer Standards:', false);
foreach ($sourceStandards as $sourceStandard) { foreach ($srcStandards as $srcStandard) {
$this->io->writeError(sprintf(' <info>%s</info>', $sourceStandard->getName())); $this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
$sourcePath = $sourceStandard->getPath(); $srcPath = $srcStandard->getPath();
$destPath = $destStandardsBasePath . DIRECTORY_SEPARATOR . $sourceStandard->getName(); $dstPath = $dstStdBasePath . DIRECTORY_SEPARATOR . $srcStandard->getName();
$filesystem->mirror($sourcePath, $destPath, null, array('override' => $override)); $filesystem->mirror($srcPath, $dstPath, null, ['override' => $override]);
} }
} }
/** /**
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
* @param PackageInterface $package * @param PackageInterface $package
*
* @return void * @return void
*/ */
protected function removeStandards(InstalledRepositoryInterface $repo, PackageInterface $package) protected function removeStandards(InstalledRepositoryInterface $repo, PackageInterface $package)
{ {
$sourceStandards = $this->getSourceStandards($package); $srcStandards = $this->getSourceStandards($package);
$destinationStandards = $this->getDestinationStandards($repo); $dstStandards = $this->getDestinationStandards($repo);
$this->io->writeError(' Removing PHP-CodeSniffer Standards:', false); $this->io->writeError(' Removing PHP-CodeSniffer Standards:', false);
foreach ($sourceStandards as $sourceStandard) { foreach ($srcStandards as $srcStandard) {
if (!$destinationStandards->hasStandard($sourceStandard)) { if (! $dstStandards->hasStandard($srcStandard)) {
continue; continue;
} }
$this->io->writeError(sprintf(' <info>%s</info>', $sourceStandard->getName())); $this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
$destinationStandard = $destinationStandards->getStandard($sourceStandard); $dstStandard = $dstStandards->getStandard($srcStandard);
$this->filesystem->removeDirectory($destinationStandard->getPath()); $this->filesystem->removeDirectory($dstStandard->getPath());
} }
} }
...@@ -137,6 +169,7 @@ class Installer extends LibraryInstaller ...@@ -137,6 +169,7 @@ class Installer extends LibraryInstaller
* Get source (provided by the composer package) standards for package. * Get source (provided by the composer package) standards for package.
* *
* @param PackageInterface $package * @param PackageInterface $package
*
* @return Standards * @return Standards
*/ */
protected function getSourceStandards(PackageInterface $package) protected function getSourceStandards(PackageInterface $package)
...@@ -150,6 +183,7 @@ class Installer extends LibraryInstaller ...@@ -150,6 +183,7 @@ class Installer extends LibraryInstaller
* Get destination (provided by PHPCodeSniffer) standards. * Get destination (provided by PHPCodeSniffer) standards.
* *
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
*
* @return Standards * @return Standards
*/ */
protected function getDestinationStandards(InstalledRepositoryInterface $repo) protected function getDestinationStandards(InstalledRepositoryInterface $repo)
...@@ -162,6 +196,7 @@ class Installer extends LibraryInstaller ...@@ -162,6 +196,7 @@ class Installer extends LibraryInstaller
/** /**
* @param Standard $source * @param Standard $source
* @param Standard $destination * @param Standard $destination
*
* @return bool * @return bool
*/ */
protected function compareStandards(Standard $source, Standard $destination) protected function compareStandards(Standard $source, Standard $destination)
...@@ -172,25 +207,17 @@ class Installer extends LibraryInstaller ...@@ -172,25 +207,17 @@ class Installer extends LibraryInstaller
/** /**
* @param string $basePath * @param string $basePath
*
* @return Standards * @return Standards
*/ */
protected function findStandards($basePath) protected function findStandards($basePath)
{ {
$standards = new Standards(); return $this->finder->in($basePath);
$finder = new Finder();
$finder
->in($basePath . '/**/Standards/*/')
->files()->name('ruleset.xml');
foreach ($finder as $ruleSetFile) {
$standard = new Standard($ruleSetFile->getPath());
$standards->addStandard($standard);
}
return $standards;
} }
/** /**
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
*
* @return string * @return string
*/ */
protected function getPHPCodeSnifferStandardsBasePath(InstalledRepositoryInterface $repo) protected function getPHPCodeSnifferStandardsBasePath(InstalledRepositoryInterface $repo)
...@@ -203,13 +230,14 @@ class Installer extends LibraryInstaller ...@@ -203,13 +230,14 @@ class Installer extends LibraryInstaller
/** /**
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
*
* @return PackageInterface * @return PackageInterface
*/ */
protected function getPHPCodeSnifferPackage(InstalledRepositoryInterface $repo) protected function getPHPCodeSnifferPackage(InstalledRepositoryInterface $repo)
{ {
$packageKey = 'squizlabs/php_codesniffer'; $packageKey = 'squizlabs/php_codesniffer';
$package = $repo->findPackage($packageKey, '*'); $package = $repo->findPackage($packageKey, '*');
if (!$package) { if (! $package) {
throw new \RuntimeException(sprintf('Package "%s" not installed.', $packageKey)); throw new \RuntimeException(sprintf('Package "%s" not installed.', $packageKey));
} }
...@@ -218,10 +246,23 @@ class Installer extends LibraryInstaller ...@@ -218,10 +246,23 @@ class Installer extends LibraryInstaller
/** /**
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
*
* @return string * @return string
*/ */
protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $repo) protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $repo)
{ {
return $this->getInstallPath($this->getPHPCodeSnifferPackage($repo)); return $this->getInstallPath($this->getPHPCodeSnifferPackage($repo));
} }
/**
* {@inheritDoc}
*/
public function supports($packageType)
{
$secondaryTypes = ['phpcodesniffer-standards'];
$deprecatedTypes = ['php-codesniffer-standards'];
return parent::supports($packageType)
|| in_array($packageType, array_merge($secondaryTypes, $deprecatedTypes));
}
} }
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standards;
use Higidi\ComposerPhpCSStandardsPlugin\Symfony\Finder\Factory as FinderFactory;
use Symfony\Component\Finder\Finder as SymfonyFinder;
/**
* Standards finder class.
*/
class Finder
{
/**
* @var FinderFactory
*/
protected $finderFactory;
/**
* @var Factory
*/
protected $factory;
/**
* @param FinderFactory|null $finderFactory
* @param Factory|null $factory
*/
public function __construct(FinderFactory $finderFactory = null, Factory $factory = null)
{
$this->finderFactory = $finderFactory ?: new FinderFactory();
$this->factory = $factory ?: new Factory();
}
/**
* Find and return PHPCodeSniffer standards.
*
* @param string $path
*
* @return Standards
*
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function in($path)
{
$finder = $this->getSymfonyFinder()
->in($path)
->files()
->name('ruleset.xml')
->sortByName();
$paths = iterator_to_array($finder, false);
$paths = array_map(
function (\SplFileInfo $file) {
return $file->getPath();
},
$paths
);
return $this->createStandardsFromPaths($paths);
}
/**
* Creates new symfony finder instance.
*
* @return SymfonyFinder
*/
protected function getSymfonyFinder()
{
return $this->finderFactory->create();
}
/**
* Creates PHPCodeSniffer standards from paths.
*
* @param array $path
*
* @return Standards
*/
protected function createStandardsFromPaths(array $paths)
{
return $this->factory->create($paths);
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer;
class Standard
{
const RULESET_FILENAME = 'ruleset.xml';
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $ruleSetXmlPath;
/**
* @param $path
*/
public function __construct($path)
{
$path = rtrim($path, DIRECTORY_SEPARATOR);
$pathParts = explode(DIRECTORY_SEPARATOR, $path);
$this->name = array_pop($pathParts);
$this->path = $path;
$this->ruleSetXmlPath = $path . DIRECTORY_SEPARATOR . 'ruleset.xml';
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return string
*/
public function getRuleSetXmlPath()
{
return $this->ruleSetXmlPath;
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory as StandardFactory;
/**
* Standards class factory.
*/
class Factory
{
/**
* @var StandardFactory
*/
protected $standardFactory;
/**
* @param StandardFactory|null $standardFactory
*/
public function __construct(StandardFactory $standardFactory = null)
{
$this->standardFactory = $standardFactory ?: new StandardFactory();
}
/**
* Creates PHPCodeSniffer standards from paths.
*
* @param array $paths Paths with PHPCodeSniffer standard paths.
*
* @return Standards PHPCodeSniffer standards object.
*/
public function create(array $paths)
{
$standards = new Standards();
foreach ($paths as $path) {
$standards->addStandard($this->standardFactory->create($path));
}
return $standards;
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Exception;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* Invalid standard exception.
*/
class InvalidStandardException extends \InvalidArgumentException
{
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Exception;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* Standard path access denied exception.
*/
class StandardPathAccessDeniedException extends \UnexpectedValueException
{
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* Standards class factory.
*/
class Factory
{
/**
* Creates PHPCodeSniffer standard from path.
*
* @param string $path Path to the PHPCodeSniffer standard.
*
* @return Standard PHPCodeSniffer standard object.
*/
public function create($path)
{
return new Standard($path);
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Exception\InvalidStandardException;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Exception\StandardPathAccessDeniedException;
/**
* Class for PHPCodeSniffer standards.
*/
class Standard implements StandardInterface
{
const RULESET_FILENAME = 'ruleset.xml';
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $ruleSetXmlPath;
/**
* @param string $path
*/
public function __construct($path)
{
if (! is_readable($path)) {
throw new StandardPathAccessDeniedException(
sprintf('Standard path "%s" is not accessable.', $path)
);
}
$this->path = realpath($path);
$this->ruleSetXmlPath = $path . DIRECTORY_SEPARATOR . static::RULESET_FILENAME;
$this->name = $this->getNameFromRuleSet($this->ruleSetXmlPath);
if (! is_readable($this->ruleSetXmlPath)) {
throw new InvalidStandardException(
sprintf('Standard "%s" doesn\'t contain a "ruleset.xml" file.', $this->name)
);
}
}
/**
* Fetch PHPCodeSniffer standard name from ruleset.xml.
*
* @param string $ruleSetXmlPath The absolute path to ruleset.xml.
*
* @return string The name of the PHPCodeSniffer standard.
*/
protected function getNameFromRuleSet($ruleSetXmlPath)
{
try {
$ruleSet = new \SimpleXMLElement(file_get_contents($ruleSetXmlPath));
$name = trim($ruleSet->attributes()['name']);
if ($name !== '') {
return $name;
}
} catch (\Exception $e) {
// Nothing todo, use folder name.
}
return basename(dirname($ruleSetXmlPath));
}
/**
* Get the name of the PHPCodeSniffer standard.
*
* @return string The name of the PHPCodeSniffer standard.
*/
public function getName()
{
return $this->name;
}
/**
* Get the path to PHPCodeSniffer standard.
*
* @return string The path to the PHPCodeSniffer standard.
*/
public function getPath()
{
return $this->path;
}
/**
* Get the path to the PHPCodeSniffer ruleset.xml file.
*
* @return string The path to the PHPCodeSniffer ruleset.xml file.
*/
public function getRuleSetXmlPath()
{
return $this->ruleSetXmlPath;
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* Interface for PHPCodeSniffer standards.
*/
interface StandardInterface
{
/**
* Get the name of the PHPCodeSniffer standard.
*
* @return string The name of the PHPCodeSniffer standard.
*/
public function getName();
/**
* Get the path to PHPCodeSniffer standard.
*
* @return string The path to the PHPCodeSniffer standard.
*/
public function getPath();
/**
* Get the path to the PHPCodeSniffer ruleset.xml file.
*
* @return string The path to the PHPCodeSniffer ruleset.xml file.
*/
public function getRuleSetXmlPath();
}
<?php <?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer; namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\StandardInterface;
/**
* Class holds a set of PHPCodeSniffer standards.
*/
class Standards implements \Iterator class Standards implements \Iterator
{ {
/** /**
* @var array|Standard[] * @var StandardInterface[]
*/ */
protected $standards; protected $standards;
/** /**
* @param array $standards * @param StandardInterface[] $standards
*/ */
public function __construct(array $standards = array()) public function __construct(array $standards = [])
{ {
foreach ($standards as $standard) { $this->setStandards($standards);
$this->addStandard($standard);
}
} }
/** /**
* @param Standard $standard * Add a single standard to set.
* @return $this *
* @param StandardInterface $standard The standard to add
*
* @return $this Fluent interface
*/ */
public function addStandard(Standard $standard) public function addStandard(StandardInterface $standard)
{ {
$this->standards[$standard->getName()] = $standard; $this->standards[$standard->getName()] = $standard;
...@@ -32,8 +56,11 @@ class Standards implements \Iterator ...@@ -32,8 +56,11 @@ class Standards implements \Iterator
} }
/** /**
* @param string|Standard $standard * Check whether set holds the given standard.
* @return bool *
* @param string|StandardInterface $standard The standard to check
*
* @return bool True if sets hold the standard, otherwise false
*/ */
public function hasStandard($standard) public function hasStandard($standard)
{ {
...@@ -41,8 +68,11 @@ class Standards implements \Iterator ...@@ -41,8 +68,11 @@ class Standards implements \Iterator
} }
/** /**
* @param string|Standard $standard * Remove a single standard from the set.
* @return $this *
* @param string|StandardInterface $standard The standard to remove
*
* @return $this Fluent interface
*/ */
public function removeStandard($standard) public function removeStandard($standard)
{ {
...@@ -54,12 +84,15 @@ class Standards implements \Iterator ...@@ -54,12 +84,15 @@ class Standards implements \Iterator
} }
/** /**
* @param string|Standard $standard * Get a single standard.
* @return Standard|null *
* @param string|StandardInterface $standard The standard to get
*
* @return StandardInterface|null Return the standard or null if not exist
*/ */
public function getStandard($standard) public function getStandard($standard)
{ {
if (!$this->hasStandard($standard)) { if (! $this->hasStandard($standard)) {
return null; return null;
} }
...@@ -67,31 +100,53 @@ class Standards implements \Iterator ...@@ -67,31 +100,53 @@ class Standards implements \Iterator
} }
/** /**
* @return array|Standard[] * Get all standards hold by this set.
*
* @return array|StandardInterface[] Array of standards
*/ */
public function getStandards() public function getStandards()
{ {
return $this->standards; return array_values($this->standards);
} }
/** /**
* @param string|Standard $standard * Set standards hold by this set.
* @return string *
* @param StandardInterface[] $standards Array with standards to set
*
* @return $this Fluent interface
*/ */
protected function getStandardName($standard) { public function setStandards(array $standards)
if ($standard instanceof Standard) { {
$standard = $standard->getName(); $this->standards = [];
} else { foreach ($standards as $standard) {
$standard = (string)$standard; $this->addStandard($standard);
} }
return $standard; return $this;
}
/**
* Extract the name of the given standard.
*
* @param string|StandardInterface $standard The standard get name from
*
* @return string The standard name
*/
protected function getStandardName($standard)
{
if ($standard instanceof StandardInterface) {
return $standard->getName();
}
return (string)$standard;
} }
/** /**
* Return the current element * Return the current element
*
* @link http://php.net/manual/en/iterator.current.php * @link http://php.net/manual/en/iterator.current.php
* @return Standard Can return any type. * @return StandardInterface Can return any type.
* @since 5.0.0 * @since 5.0.0
*/ */
public function current() public function current()
...@@ -101,6 +156,7 @@ class Standards implements \Iterator ...@@ -101,6 +156,7 @@ class Standards implements \Iterator
/** /**
* Move forward to next element * Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php * @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored. * @return void Any returned value is ignored.
* @since 5.0.0 * @since 5.0.0
...@@ -112,17 +168,21 @@ class Standards implements \Iterator ...@@ -112,17 +168,21 @@ class Standards implements \Iterator
/** /**
* Return the key of the current element * Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php * @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure. * @return mixed scalar on success, or null on failure.
* @since 5.0.0 * @since 5.0.0
*/ */
public function key() public function key()
{ {
return key($this->standards); $key = key($this->standards);
return array_search($key, array_keys($this->standards));
} }
/** /**
* Checks if current position is valid * Checks if current position is valid
*
* @link http://php.net/manual/en/iterator.valid.php * @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated. * @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure. * Returns true on success or false on failure.
...@@ -135,6 +195,7 @@ class Standards implements \Iterator ...@@ -135,6 +195,7 @@ class Standards implements \Iterator
/** /**
* Rewind the Iterator to the first element * Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php * @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored. * @return void Any returned value is ignored.
* @since 5.0.0 * @since 5.0.0
......
...@@ -2,17 +2,41 @@ ...@@ -2,17 +2,41 @@
namespace Higidi\ComposerPhpCSStandardsPlugin; namespace Higidi\ComposerPhpCSStandardsPlugin;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Composer\Composer; use Composer\Composer;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface; use Composer\Plugin\PluginInterface;
/**
* Plugin class register our plugin with composer.
*/
class Plugin implements PluginInterface class Plugin implements PluginInterface
{ {
/** /**
* @param Composer $composer * @param Composer $composer
* @param IOInterface $io * @param IOInterface $io
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.ShortVariable)
*/ */
public function activate(Composer $composer, IOInterface $io) public function activate(Composer $composer, IOInterface $io)
{ {
......
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\Symfony\Finder;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Symfony\Component\Finder\Finder;
/**
* Symfony finder factory class.
*/
class Factory
{
/**
* Creates new symfony finder instance.
*
* @return Finder
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function create()
{
return Finder::create();
}
}