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:
- lint
- test
test:5.3: &PHP-UNITTESTING
image: tetraweb/php:5.3
cache:
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
tags:
- docker
variables:
TIMEZONE: "Europe/Berlin"
WITH_XDEBUG: "true"
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
script:
- ./vendor/bin/phpunit
- ./vendor/bin/phpunit --coverage-text --colors=never
test:5.4:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:5.4
test:5.5:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:5.5
test:5.6:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:5.6
test:7.0:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:7.0
test:7.1:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:7.1
test:latest:
<<: *PHP-UNITTESTING
<<: *test
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
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
------------
_TODO_
- `PHP >= 5.4`
- Composer package providing the PHP CodeSniffer Rulesets & Sniffs
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
------
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 @@
"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": {
"psr-4": {
"Higidi\\ComposerPhpCSStandardsPlugin\\": "src/"
......@@ -24,10 +28,11 @@
},
"autoload-dev": {
"psr-4": {
"Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/src/"
"Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/"
}
},
"require": {
"php": "^5.4 || ^7.0",
"composer-plugin-api": "^1.1",
"squizlabs/php_codesniffer": "^2.0",
"symfony/finder": "^2.1 || ^3.0",
......@@ -35,9 +40,13 @@
},
"require-dev": {
"composer/composer": "^1.4",
"phpunit/phpunit": "^4.5 || ^5.0.5"
"phpunit/phpunit": "^4.8.36 || ^5.0.5",
"phpmd/phpmd": "^2.6"
},
"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 @@
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Project Test Suite">
<directory>./tests/src/</directory>
<testsuite name="Unit Tests">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="Integration Tests">
<directory>./tests/Integration/</directory>
</testsuite>
</testsuites>
......
......@@ -2,6 +2,25 @@
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\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller;
......@@ -9,14 +28,19 @@ use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
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 Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standard;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
class Installer extends LibraryInstaller
{
const TYPE = 'php-codesniffer-standards';
const TYPE = 'phpcodesniffer-standard';
/**
* @var Finder
*/
protected $finder;
/**
* Initializes library installer.
......@@ -26,15 +50,19 @@ class Installer extends LibraryInstaller
* @param string $type
* @param Filesystem $filesystem
* @param BinaryInstaller $binaryInstaller
*
* @SuppressWarnings(PHPMD.ShortVariable)
*/
public function __construct(
IOInterface $io,
Composer $composer,
$type = self::TYPE,
Filesystem $filesystem = null,
BinaryInstaller $binaryInstaller = null
BinaryInstaller $binaryInstaller = null,
Finder $finder = null
) {
parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller);
$this->finder = $finder ?: new Finder();
}
/**
......@@ -42,23 +70,21 @@ class Installer extends LibraryInstaller
*/
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
{
$isInstalled = parent::isInstalled($repo, $package);
if (!$isInstalled) {
return $isInstalled;
if (! parent::isInstalled($repo, $package)) {
return false;
}
$sourceStandards = $this->getSourceStandards($package);
$destinationStandards = $this->getDestinationStandards($repo);
$srcStandards = $this->getSourceStandards($package);
$dstStandards = $this->getDestinationStandards($repo);
foreach ($sourceStandards as $sourceStandard) {
if (!$destinationStandards->hasStandard($sourceStandard)
|| !$this->compareStandards($sourceStandard, $destinationStandards->getStandard($sourceStandard))
foreach ($srcStandards as $srcStandard) {
if (! $dstStandards->hasStandard($srcStandard)
|| ! $this->compareStandards($srcStandard, $dstStandards->getStandard($srcStandard))
) {
$isInstalled = false;
break;
return false;
}
}
return $isInstalled;
return true;
}
/**
......@@ -79,14 +105,13 @@ class Installer extends LibraryInstaller
$this->installStandards($repo, $target, $initial ? false : true);
}
/**
* {@inheritDoc}
*/
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!$repo->hasPackage($package)) {
throw new \InvalidArgumentException('Package is not installed: '.$package);
if (! $repo->hasPackage($package)) {
throw new \InvalidArgumentException('Package is not installed: ' . $package);
}
$this->removeStandards($repo, $package);
parent::uninstall($repo, $package);
......@@ -96,40 +121,47 @@ class Installer extends LibraryInstaller
* @param InstalledRepositoryInterface $repo
* @param PackageInterface $package
* @param bool $override
*
* @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();
$sourceStandards = $this->getSourceStandards($package);
$destStandardsBasePath = $this->getPHPCodeSnifferStandardsBasePath($repo);
$srcStandards = $this->getSourceStandards($package);
$dstStdBasePath = $this->getPHPCodeSnifferStandardsBasePath($repo);
$this->io->writeError(' Installing PHP-CodeSniffer Standards:', false);
foreach ($sourceStandards as $sourceStandard) {
$this->io->writeError(sprintf(' <info>%s</info>', $sourceStandard->getName()));
$sourcePath = $sourceStandard->getPath();
$destPath = $destStandardsBasePath . DIRECTORY_SEPARATOR . $sourceStandard->getName();
$filesystem->mirror($sourcePath, $destPath, null, array('override' => $override));
foreach ($srcStandards as $srcStandard) {
$this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
$srcPath = $srcStandard->getPath();
$dstPath = $dstStdBasePath . DIRECTORY_SEPARATOR . $srcStandard->getName();
$filesystem->mirror($srcPath, $dstPath, null, ['override' => $override]);
}
}
/**
* @param InstalledRepositoryInterface $repo
* @param PackageInterface $package
*
* @return void
*/
protected function removeStandards(InstalledRepositoryInterface $repo, PackageInterface $package)
{
$sourceStandards = $this->getSourceStandards($package);
$destinationStandards = $this->getDestinationStandards($repo);
$srcStandards = $this->getSourceStandards($package);
$dstStandards = $this->getDestinationStandards($repo);
$this->io->writeError(' Removing PHP-CodeSniffer Standards:', false);
foreach ($sourceStandards as $sourceStandard) {
if (!$destinationStandards->hasStandard($sourceStandard)) {
foreach ($srcStandards as $srcStandard) {
if (! $dstStandards->hasStandard($srcStandard)) {
continue;
}
$this->io->writeError(sprintf(' <info>%s</info>', $sourceStandard->getName()));
$destinationStandard = $destinationStandards->getStandard($sourceStandard);
$this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
$dstStandard = $dstStandards->getStandard($srcStandard);
$this->filesystem->removeDirectory($destinationStandard->getPath());
$this->filesystem->removeDirectory($dstStandard->getPath());
}
}
......@@ -137,6 +169,7 @@ class Installer extends LibraryInstaller
* Get source (provided by the composer package) standards for package.
*
* @param PackageInterface $package
*
* @return Standards
*/
protected function getSourceStandards(PackageInterface $package)
......@@ -150,6 +183,7 @@ class Installer extends LibraryInstaller
* Get destination (provided by PHPCodeSniffer) standards.
*
* @param InstalledRepositoryInterface $repo
*
* @return Standards
*/
protected function getDestinationStandards(InstalledRepositoryInterface $repo)
......@@ -162,6 +196,7 @@ class Installer extends LibraryInstaller
/**
* @param Standard $source
* @param Standard $destination
*
* @return bool
*/
protected function compareStandards(Standard $source, Standard $destination)
......@@ -172,25 +207,17 @@ class Installer extends LibraryInstaller
/**
* @param string $basePath
*
* @return Standards
*/
protected function findStandards($basePath)
{
$standards = new Standards();
$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;
return $this->finder->in($basePath);
}
/**
* @param InstalledRepositoryInterface $repo
*
* @return string
*/
protected function getPHPCodeSnifferStandardsBasePath(InstalledRepositoryInterface $repo)
......@@ -203,13 +230,14 @@ class Installer extends LibraryInstaller
/**
* @param InstalledRepositoryInterface $repo
*
* @return PackageInterface
*/
protected function getPHPCodeSnifferPackage(InstalledRepositoryInterface $repo)
{
$packageKey = 'squizlabs/php_codesniffer';
$package = $repo->findPackage($packageKey, '*');
if (!$package) {
if (! $package) {
throw new \RuntimeException(sprintf('Package "%s" not installed.', $packageKey));
}
......@@ -218,10 +246,23 @@ class Installer extends LibraryInstaller
/**
* @param InstalledRepositoryInterface $repo
*
* @return string
*/
protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $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
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
{
/**
* @var array|Standard[]
* @var StandardInterface[]
*/
protected $standards;
/**
* @param array $standards
* @param StandardInterface[] $standards
*/
public function __construct(array $standards = array())
public function __construct(array $standards = [])
{
foreach ($standards as $standard) {
$this->addStandard($standard);
}
$this->setStandards($standards);
}
/**
* @param Standard $standard
* @return $this
* Add a single standard to set.
*
* @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;
......@@ -32,8 +56,11 @@ class Standards implements \Iterator
}
/**
* @param string|Standard $standard
* @return bool
* Check whether set holds the given standard.
*
* @param string|StandardInterface $standard The standard to check
*
* @return bool True if sets hold the standard, otherwise false
*/
public function hasStandard($standard)
{
......@@ -41,8 +68,11 @@ class Standards implements \Iterator
}
/**
* @param string|Standard $standard
* @return $this
* Remove a single standard from the set.
*
* @param string|StandardInterface $standard The standard to remove
*
* @return $this Fluent interface
*/
public function removeStandard($standard)
{
......@@ -54,12 +84,15 @@ class Standards implements \Iterator
}
/**
* @param string|Standard $standard
* @return Standard|null
* Get a single standard.
*
* @param string|StandardInterface $standard The standard to get
*
* @return StandardInterface|null Return the standard or null if not exist
*/
public function getStandard($standard)
{
if (!$this->hasStandard($standard)) {
if (! $this->hasStandard($standard)) {
return null;
}
......@@ -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()
{
return $this->standards;
return array_values($this->standards);
}
/**
* @param string|Standard $standard
* @return string
* Set standards hold by this set.
*
* @param StandardInterface[] $standards Array with standards to set
*
* @return $this Fluent interface
*/
protected function getStandardName($standard) {
if ($standard instanceof Standard) {
$standard = $standard->getName();
} else {
$standard = (string)$standard;
public function setStandards(array $standards)
{
$this->standards = [];
foreach ($standards as $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
*
* @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
*/
public function current()
......@@ -101,6 +156,7 @@ class Standards implements \Iterator
/**
* Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
......@@ -112,17 +168,21 @@ class Standards implements \Iterator
/**
* Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
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
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
......@@ -135,6 +195,7 @@ class Standards implements \Iterator
/**
* Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
......
......@@ -2,17 +2,41 @@
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\IO\IOInterface;
use Composer\Plugin\PluginInterface;
/**
* Plugin class register our plugin with composer.
*/
class Plugin implements PluginInterface
{
/**
* @param Composer $composer
* @param Composer $composer
* @param IOInterface $io
*
* @return void
*
* @SuppressWarnings(PHPMD.ShortVariable)
*/
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();
}
}