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 (79)
Showing
with 638 additions and 89 deletions
stages: stages:
- lint
- test - test
cache: cache:
paths: paths:
- vendor/ - vendor/
before_script: .lint: &lint
- apt-get update; apt-get install -y unzip image: composer
- composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist stage: lint
before_script:
lint:coding-guideline: &PHP-LINTING - apk --no-cache add parallel
image: tetraweb/php:7.0 - php -v
stage: test - composer --no-ansi self-update
script: - composer --no-ansi --version
- mkdir result - composer install --optimize-autoloader --classmap-authoritative --no-interaction --prefer-dist --no-ansi
- ./vendor/bin/phpcs -s -n --report-full=result/phpcs-full.txt --report-diff=result/phpcs-diff.txt --report-summary=result/phpcs-summary.txt
artifacts: artifacts:
when: on_failure when: on_failure
paths: paths:
- result - 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:php-mass-detection:
<<: *PHP-LINTING <<: *lint
script: script:
- mkdir result - mkdir result
- ./vendor/bin/phpmd src text phpmd.xml | tee result/phpmd.txt - ./vendor/bin/phpmd src text phpmd.xml | tee result/phpmd.txt
test:5.3: &PHP-UNITTESTING .test: &test
image: tetraweb/php:5.3
stage: test stage: test
tags: tags:
- docker - docker
variables: variables:
TIMEZONE: "Europe/Berlin" TIMEZONE: "Europe/Berlin"
WITH_XDEBUG: "true" WITH_XDEBUG: "true"
before_script:
- apt-get update; apt-get install -y unzip
- composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist
script: script:
- ./vendor/bin/phpunit --coverage-text --colors=never - ./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
...@@ -8,7 +8,7 @@ Composer plugin installs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_Code ...@@ -8,7 +8,7 @@ Composer plugin installs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_Code
Requirements Requirements
------------ ------------
- `PHP >= 5.3.3` - `PHP >= 5.4`
- Composer package providing the PHP CodeSniffer Rulesets & Sniffs - Composer package providing the PHP CodeSniffer Rulesets & Sniffs
Installation Installation
......
...@@ -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,7 +40,7 @@ ...@@ -35,7 +40,7 @@
}, },
"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" "phpmd/phpmd": "^2.6"
}, },
"extra": { "extra": {
......
...@@ -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>
......
...@@ -22,21 +22,26 @@ namespace Higidi\ComposerPhpCSStandardsPlugin; ...@@ -22,21 +22,26 @@ namespace Higidi\ComposerPhpCSStandardsPlugin;
*/ */
use Composer\Composer; use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Installer\BinaryInstaller; use Composer\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller; use Composer\Installer\LibraryInstaller;
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 Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standard; use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards; 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 Symfony\Component\Finder\Finder;
class Installer extends LibraryInstaller class Installer extends LibraryInstaller
{ {
const TYPE = 'phpcodesniffer-standard'; const TYPE = 'phpcodesniffer-standard';
/**
* @var Finder
*/
protected $finder;
/** /**
* Initializes library installer. * Initializes library installer.
* *
...@@ -53,9 +58,11 @@ class Installer extends LibraryInstaller ...@@ -53,9 +58,11 @@ class Installer extends LibraryInstaller
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();
} }
/** /**
...@@ -63,15 +70,15 @@ class Installer extends LibraryInstaller ...@@ -63,15 +70,15 @@ class Installer extends LibraryInstaller
*/ */
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
{ {
if (!parent::isInstalled($repo, $package)) { if (! parent::isInstalled($repo, $package)) {
return false; return false;
} }
$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))
) { ) {
return false; return false;
} }
...@@ -103,7 +110,7 @@ class Installer extends LibraryInstaller ...@@ -103,7 +110,7 @@ class Installer extends LibraryInstaller
*/ */
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);
...@@ -114,7 +121,10 @@ class Installer extends LibraryInstaller ...@@ -114,7 +121,10 @@ 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( protected function installStandards(
InstalledRepositoryInterface $repo, InstalledRepositoryInterface $repo,
...@@ -122,35 +132,36 @@ class Installer extends LibraryInstaller ...@@ -122,35 +132,36 @@ class Installer extends LibraryInstaller
$override = false $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());
} }
} }
...@@ -158,6 +169,7 @@ class Installer extends LibraryInstaller ...@@ -158,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)
...@@ -171,6 +183,7 @@ class Installer extends LibraryInstaller ...@@ -171,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)
...@@ -183,6 +196,7 @@ class Installer extends LibraryInstaller ...@@ -183,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)
...@@ -193,25 +207,17 @@ class Installer extends LibraryInstaller ...@@ -193,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)
...@@ -224,13 +230,14 @@ class Installer extends LibraryInstaller ...@@ -224,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));
} }
...@@ -239,6 +246,7 @@ class Installer extends LibraryInstaller ...@@ -239,6 +246,7 @@ class Installer extends LibraryInstaller
/** /**
* @param InstalledRepositoryInterface $repo * @param InstalledRepositoryInterface $repo
*
* @return string * @return string
*/ */
protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $repo) protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $repo)
...@@ -251,8 +259,8 @@ class Installer extends LibraryInstaller ...@@ -251,8 +259,8 @@ class Installer extends LibraryInstaller
*/ */
public function supports($packageType) public function supports($packageType)
{ {
$secondaryTypes = array('phpcodesniffer-standards'); $secondaryTypes = ['phpcodesniffer-standards'];
$deprecatedTypes = array('php-codesniffer-standards'); $deprecatedTypes = ['php-codesniffer-standards'];
return parent::supports($packageType) return parent::supports($packageType)
|| in_array($packageType, array_merge($secondaryTypes, $deprecatedTypes)); || 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 <?php
namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer; namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
/* /*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de> * Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
...@@ -21,58 +21,40 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer; ...@@ -21,58 +21,40 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
class Standard use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory as StandardFactory;
{
const RULESET_FILENAME = 'ruleset.xml';
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $ruleSetXmlPath;
/**
* Standards class factory.
*/
class Factory
{
/** /**
* @param $path * @var StandardFactory
*/ */
public function __construct($path) protected $standardFactory;
{
$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 * @param StandardFactory|null $standardFactory
*/ */
public function getName() public function __construct(StandardFactory $standardFactory = null)
{ {
return $this->name; $this->standardFactory = $standardFactory ?: new StandardFactory();
} }
/** /**
* @return string * Creates PHPCodeSniffer standards from paths.
*
* @param array $paths Paths with PHPCodeSniffer standard paths.
*
* @return Standards PHPCodeSniffer standards object.
*/ */
public function getPath() public function create(array $paths)
{ {
return $this->path; $standards = new Standards();
} foreach ($paths as $path) {
$standards->addStandard($this->standardFactory->create($path));
}
/** return $standards;
* @return string
*/
public function getRuleSetXmlPath()
{
return $this->ruleSetXmlPath;
} }
} }
<?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> * Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
...@@ -21,29 +21,34 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer; ...@@ -21,29 +21,34 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer;
* 02110-1301, USA. * 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;
...@@ -51,8 +56,11 @@ class Standards implements \Iterator ...@@ -51,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)
{ {
...@@ -60,8 +68,11 @@ class Standards implements \Iterator ...@@ -60,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)
{ {
...@@ -73,12 +84,15 @@ class Standards implements \Iterator ...@@ -73,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;
} }
...@@ -86,20 +100,42 @@ class Standards implements \Iterator ...@@ -86,20 +100,42 @@ 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
*/
public function setStandards(array $standards)
{
$this->standards = [];
foreach ($standards as $standard) {
$this->addStandard($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) protected function getStandardName($standard)
{ {
if ($standard instanceof Standard) { if ($standard instanceof StandardInterface) {
return $standard->getName(); return $standard->getName();
} }
...@@ -108,8 +144,9 @@ class Standards implements \Iterator ...@@ -108,8 +144,9 @@ class Standards implements \Iterator
/** /**
* 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()
...@@ -119,6 +156,7 @@ class Standards implements \Iterator ...@@ -119,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
...@@ -130,17 +168,21 @@ class Standards implements \Iterator ...@@ -130,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.
...@@ -153,6 +195,7 @@ class Standards implements \Iterator ...@@ -153,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
......
...@@ -25,6 +25,9 @@ use Composer\Composer; ...@@ -25,6 +25,9 @@ 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
{ {
/** /**
......
<?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();
}
}
<?xml version="1.0"?>
<ruleset name="CustomStandard">
</ruleset>