-
Notifications
You must be signed in to change notification settings - Fork 2
Start with PSR-1 #1
Changes from 7 commits
1acbda6
7efbe3f
1979bd0
f638519
b9b3942
fee6a87
1f95bfb
e431d44
f47dfc9
9a8378d
48955b2
82f73d1
3f3180b
350ec47
d93b055
44bfd4a
3259c5a
c0d0000
cdddeda
6a3e68b
f03df17
87f919b
1f296ad
753d1b1
ca2db28
5db81cf
e557817
3c893dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /*.* export-ignore | ||
| /tests export-ignore |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /.phpcs-cache | ||
| /composer.lock | ||
| /phpcs.xml | ||
| /phpunit.xml | ||
| /vendor/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| sudo: false | ||
|
|
||
| language: php | ||
|
|
||
| php: | ||
| - 7.2 | ||
| - nightly | ||
|
|
||
| before_install: | ||
| - phpenv config-rm xdebug.ini || true | ||
| - phpenv config-add .travis/php.ini | ||
|
|
||
| install: | ||
| - travis_retry composer install --classmap-authoritative --no-suggest --prefer-dist | ||
|
|
||
| script: | ||
| - vendor/bin/phpunit | ||
|
|
||
| jobs: | ||
| include: | ||
|
|
||
| - stage: Test | ||
| env: DEPENDENCIES=low | ||
| php: 7.2 | ||
| install: | ||
| - travis_retry composer update --classmap-authoritative --no-suggest --prefer-dist --prefer-lowest --prefer-stable | ||
|
|
||
| - stage: Code Quality | ||
| env: CODING_STANDARDS | ||
| script: | ||
| - vendor/bin/phpcs | ||
|
||
|
|
||
| allow_failures: | ||
| - php: nightly | ||
|
|
||
| cache: | ||
| directories: | ||
| - $HOME/.composer/cache/files | ||
|
|
||
| branches: | ||
| only: | ||
| - master | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| short_open_tag = On |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "libero/coding-standard", | ||
| "description": "Libero PHP coding standard", | ||
| "type": "phpcodesniffer-standard", | ||
| "license": "MIT", | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "tests\\Libero\\CodingStandard\\": "tests/" | ||
| } | ||
| }, | ||
| "require": { | ||
| "php": "^7.2", | ||
| "dealerdirect/phpcodesniffer-composer-installer": "^0.4", | ||
| "squizlabs/php_codesniffer": "^3.3.1" | ||
| }, | ||
| "require-dev": { | ||
| "lstrojny/functional-php": "^1.8", | ||
| "phpunit/phpunit": "^7.3", | ||
| "symfony/finder": "^4.1" | ||
| }, | ||
| "config": { | ||
| "sort-packages": true | ||
| }, | ||
| "extra": { | ||
| "branch-alias": { | ||
| "dev-master": "1.0.x-dev" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> | ||
|
|
||
| <arg name="basepath" value="."/> | ||
| <arg name="cache" value=".phpcs-cache"/> | ||
| <arg name="colors"/> | ||
| <arg name="extensions" value="php"/> | ||
| <arg value="s"/> | ||
|
|
||
| <rule ref="Libero"/> | ||
|
|
||
| <file>src/</file> | ||
| <file>tests/</file> | ||
|
|
||
| <rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"> | ||
| <exclude-pattern>*/tests/*</exclude-pattern> | ||
| </rule> | ||
|
|
||
| </ruleset> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.3/phpunit.xsd" colors="true" | ||
| bootstrap="vendor/autoload.php"> | ||
|
|
||
| <php> | ||
| <const name="PHP_CODESNIFFER_CBF" value="true"/> | ||
| </php> | ||
|
|
||
| <testsuites> | ||
| <testsuite name="Libero Coding Standard"> | ||
| <directory suffix=".php">tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <filter> | ||
| <whitelist> | ||
| <directory>src</directory> | ||
| </whitelist> | ||
| </filter> | ||
|
|
||
| </phpunit> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <ruleset name="Libero" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="../../vendor/squizlabs/php_codesniffer/phpcs.xsd"> | ||
|
|
||
| <description>The Libero coding standard.</description> | ||
|
|
||
| <rule ref="PSR2"/> | ||
|
|
||
| </ruleset> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <?php | ||
|
|
||
| namespace tests\Libero\CodingStandard; | ||
|
|
||
| use LogicException; | ||
| use PHP_CodeSniffer\Config; | ||
| use PHP_CodeSniffer\Files\DummyFile; | ||
| use PHP_CodeSniffer\Files\File; | ||
| use PHP_CodeSniffer\Runner; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Finder\Finder; | ||
| use function array_combine; | ||
| use function array_filter; | ||
| use function array_map; | ||
| use function explode; | ||
| use function Functional\flatten; | ||
| use function ini_get; | ||
| use function preg_match_all; | ||
| use function sort; | ||
| use function strpos; | ||
|
|
||
| final class Tests extends TestCase | ||
|
||
| { | ||
| /** @var Runner */ | ||
| private static $codeSniffer; | ||
|
|
||
| public static function setUpBeforeClass() | ||
| { | ||
| self::$codeSniffer = new Runner(); | ||
| self::$codeSniffer->config = new Config(['--standard=Libero']); | ||
| self::$codeSniffer->init(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * @dataProvider cases | ||
| */ | ||
| public function it_finds_and_fixes_violations( | ||
| string $filename, | ||
| string $contents, | ||
| string $fixed, | ||
| array $messages, | ||
| ?string $description | ||
| ) : void { | ||
| $file = $this->createFile($filename, $contents); | ||
| $actual = flatten($this->getMessages($file)); | ||
|
|
||
| sort($actual); | ||
| sort($messages); | ||
|
|
||
| $this->assertSame($messages, $actual, $description); | ||
| $this->assertSame($fixed, $file->fixer->getContents()); | ||
| } | ||
|
|
||
| public function cases() : iterable | ||
| { | ||
| $files = Finder::create()->files()->in(__DIR__.'/cases'); | ||
|
|
||
| foreach ($files as $file) { | ||
| preg_match_all('~(?:---)?([A-Z]+)---\s+([\s\S]+?)\n---~', $file->getContents(), $matches); | ||
|
|
||
| $parts = array_combine(array_map('strtolower', $matches[1]), $matches[2]); | ||
|
|
||
| if (isset($parts['messages'])) { | ||
| $parts['messages'] = array_filter(explode("\n", $parts['messages'])); | ||
| } | ||
|
|
||
| if (empty($parts['contents'])) { | ||
| throw new LogicException("Couldn't find contents in {$file->getRelativePathname()}"); | ||
| } elseif (empty($parts['fixed']) && empty($parts['messages'])) { | ||
| throw new LogicException("Expected one of fixed or messages in {$file->getRelativePathname()}"); | ||
| } | ||
|
|
||
| yield $file->getRelativePathname() => [ | ||
| $parts['filename'] ?? 'test.php', | ||
| $parts['contents'], | ||
| $parts['fixed'] ?? $parts['contents'], | ||
| $parts['messages'] ?? [], | ||
| $parts['description'] ?? null, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| private function createFile(string $filename, string $content) : File | ||
| { | ||
| if (!ini_get('short_open_tag') && false === strpos($content, '<?php')) { | ||
| $this->markTestSkipped('short_open_tag option is disabled'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given these tests should run in a controlled CI environment and are the only ones in the library,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More meant for running the tests locally. |
||
| } | ||
|
|
||
| $file = new DummyFile( | ||
| "phpcs_input_file:${filename}\n{$content}", | ||
| self::$codeSniffer->ruleset, | ||
| self::$codeSniffer->config | ||
| ); | ||
|
|
||
| $file->process(); | ||
|
|
||
| $file->fixer->fixFile(); | ||
|
|
||
| $file = new DummyFile( | ||
| "phpcs_input_file:${filename}\n{$file->fixer->getContents()}", | ||
| self::$codeSniffer->ruleset, | ||
| self::$codeSniffer->config | ||
| ); | ||
|
|
||
| $file->process(); | ||
|
|
||
| return $file; | ||
| } | ||
|
|
||
| private function getMessages(File $file) : iterable | ||
| { | ||
| foreach ([$file->getErrors(), $file->getWarnings()] as $messages) { | ||
| foreach ($messages as $line => $lineMessages) { | ||
| foreach ($lineMessages as $column => $columnMessages) { | ||
| foreach ($columnMessages as $data) { | ||
| yield "{$line}:{$column} {$data['message']}"; | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| namespace tests\Libero\CodingStandard; | ||
|
|
||
| require __DIR__.'/../vendor/autoload.php'; | ||
| require __DIR__.'/../vendor/squizlabs/php_codesniffer/autoload.php'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ---DESCRIPTION--- | ||
| Filename must match class name | ||
| ---FILENAME--- | ||
| foobar.php | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| namespace Vendor; | ||
|
|
||
| class foobar | ||
| { | ||
| } | ||
|
|
||
| ---MESSAGES--- | ||
| 5:1 Class name "foobar" is not in PascalCase format | ||
|
||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ---DESCRIPTION--- | ||
| Filename must match class name | ||
| ---FILENAME--- | ||
| foobar.php | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| namespace Vendor; | ||
|
|
||
| interface foobar | ||
| { | ||
| } | ||
|
|
||
| ---MESSAGES--- | ||
| 5:1 Interface name "foobar" is not in PascalCase format | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ---DESCRIPTION--- | ||
| No closing PHP tags at the end of files | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
| ?> | ||
|
|
||
| ---FIXED--- | ||
| <?php | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
|
||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ---DESCRIPTION--- | ||
| Lines must not exceed 120 characters | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| $foo = '890123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789'; | ||
|
|
||
| ---MESSAGES--- | ||
| 3:121 Line exceeds 120 characters; contains 121 characters | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ---DESCRIPTION--- | ||
| PHP short tags are not allowed | ||
| ---CONTENTS--- | ||
| <? | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
| ---FIXED--- | ||
| <?php | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ---DESCRIPTION--- | ||
| No closing PHP tags at the end of files | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
| ?> | ||
|
|
||
| ---FIXED--- | ||
| <?php | ||
|
|
||
| $foo = 'bar'; | ||
|
|
||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ---DESCRIPTION--- | ||
| Filename must match class name | ||
| ---FILENAME--- | ||
| foobar.php | ||
| ---CONTENTS--- | ||
| <?php | ||
|
|
||
| namespace Vendor; | ||
|
|
||
| trait foobar | ||
| { | ||
| } | ||
|
|
||
| ---MESSAGES--- | ||
| 5:1 Trait name "foobar" is not in PascalCase format | ||
| --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
important notice: outsources the maintenance of the PHP environments to Travis CI for the library to be cross-checked across PHP versions