From bb3cf2e5943a9773477e34ef0cd1337cb54c1d26 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 18 Jan 2021 13:21:29 +0100 Subject: [PATCH] Use Github actions with style (#233) * Update style * Update CI rules * Normalize composer.json * Disable yoda * Apply CS fixes --- .github/workflows/ci.yaml | 32 ++++++----------- .gitignore | 1 + .php_cs | 24 +++++++++++++ composer.json | 16 ++++----- tests/AssertTest.php | 20 +++++------ tests/BaseTestCase.php | 8 +++-- tests/ProjectCodeTest.php | 34 ++++++++++--------- tests/static-analysis/assert-isNotA.php | 2 +- tests/static-analysis/assert-keyNotExists.php | 1 - tests/static-analysis/assert-notEmpty.php | 1 - .../static-analysis/assert-notInstanceOf.php | 2 +- 11 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 .php_cs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 80e2fa8f..a847bd61 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,11 +17,6 @@ jobs: runs-on: "ubuntu-latest" - strategy: - matrix: - php-version: - - "7.2" - steps: - name: "Checkout" uses: "actions/checkout@v2.3.4" @@ -31,25 +26,25 @@ jobs: with: coverage: "none" extensions: "mbstring" - php-version: "${{ matrix.php-version }}" + php-version: "7.4" + tools: "composer-normalize" - - name: "Validate composer.json and composer.lock" + - name: "Validate composer.json" run: "composer validate --strict" + - name: "Normalize composer.json" + run: "composer-normalize --dry-run" - - name: "Install dependencies with composer" - run: "composer update --no-interaction --no-progress" + - name: "PHP-CS-Fixer" + uses: "docker://oskarstark/php-cs-fixer-ga:2.18.0" + with: + args: "--dry-run --diff-format udiff" static-code-analysis: name: "Static Code Analysis" runs-on: "ubuntu-latest" - strategy: - matrix: - php-version: - - "7.4" - steps: - name: "Checkout" uses: "actions/checkout@v2.3.4" @@ -59,7 +54,7 @@ jobs: with: coverage: "none" extensions: "mbstring" - php-version: "${{ matrix.php-version }}" + php-version: "7.4" - name: "Install dependencies with composer" run: "composer update --no-interaction --no-progress && composer i --working-dir=ci" @@ -135,11 +130,6 @@ jobs: runs-on: "ubuntu-latest" - strategy: - matrix: - php-version: - - "7.4" - steps: - name: "Checkout" uses: "actions/checkout@v2.3.4" @@ -148,7 +138,7 @@ jobs: with: coverage: "pcov" extensions: "mbstring" - php-version: "${{ matrix.php-version }}" + php-version: "7.4" - name: "Install dependencies with composer" run: "composer update --no-interaction --no-progress" diff --git a/.gitignore b/.gitignore index 71dec7ce..c2a741aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ /composer.lock .phpunit.result.cache +.php_cs.cache diff --git a/.php_cs b/.php_cs new file mode 100644 index 00000000..f7afd3ed --- /dev/null +++ b/.php_cs @@ -0,0 +1,24 @@ +in(__DIR__.'/src') + ->in(__DIR__.'/tests') +; + +return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) + ->setRules([ + '@PSR2' => true, + '@Symfony' => true, + 'ordered_imports' => true, + 'array_syntax' => ['syntax' => 'long'], + 'no_superfluous_phpdoc_tags' => false, + 'phpdoc_annotation_without_dot' => false, + 'phpdoc_types_order' => false, + 'phpdoc_summary' => false, + 'phpdoc_to_comment' => false, + 'phpdoc_align' => false, + 'yoda_style' => false, + ]) + ->setFinder($finder) +; diff --git a/composer.json b/composer.json index d15f481c..90353e46 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,17 @@ "php": "^5.3.3 || ^7.0 || ^8.0", "symfony/polyfill-ctype": "^1.8" }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<3.9.1" + }, "require-dev": { "phpunit/phpunit": "^4.8.36 || ^7.5.13 || ^8.5.13" }, - "conflict": { - "vimeo/psalm": "<3.9.1", - "phpstan/phpstan": "<0.12.20" + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } }, "autoload": { "psr-4": { @@ -34,10 +39,5 @@ "Webmozart\\Assert\\Tests\\": "tests/", "Webmozart\\Assert\\Bin\\": "bin/src" } - }, - "extra": { - "branch-alias": { - "dev-master": "1.10-dev" - } } } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index d6eea0e6..15527cfa 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -13,8 +13,8 @@ use ArrayIterator; use ArrayObject; -use Exception; use Error; +use Exception; use LogicException; use RuntimeException; use stdClass; @@ -516,12 +516,12 @@ public function getTests() array('uuid', array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), false), array('uuid', array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), false), array('uuid', array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), false), - array('throws', array(function() { throw new LogicException('test'); }, 'LogicException'), true), - array('throws', array(function() { throw new LogicException('test'); }, 'IllogicException'), false), - array('throws', array(function() { throw new Exception('test'); }), true), - array('throws', array(function() { trigger_error('test'); }, 'Throwable'), true, false, 70000), - array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000), - array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000), + array('throws', array(function () { throw new LogicException('test'); }, 'LogicException'), true), + array('throws', array(function () { throw new LogicException('test'); }, 'IllogicException'), false), + array('throws', array(function () { throw new Exception('test'); }), true), + array('throws', array(function () { trigger_error('test'); }, 'Throwable'), true, false, 70000), + array('throws', array(function () { trigger_error('test'); }, 'Unthrowable'), false, false, 70000), + array('throws', array(function () { throw new Error(); }, 'Throwable'), true, true, 70000), array('ip', array('192.168.0.1'), true), array('ip', array(new ToStringClass('192.168.0.1')), true), array('ip', array('255.255.255.255'), true), @@ -698,7 +698,7 @@ public function getStringConversions() array('string', array(self::getResource()), 'Expected a string. Got: resource'), array('eq', array('1', '2'), 'Expected a value equal to "2". Got: "1"'), - array('eq', array(new ToStringClass("XXX"), new ToStringClass("YYY")), 'Expected a value equal to Webmozart\Assert\Tests\ToStringClass: "YYY". Got: Webmozart\Assert\Tests\ToStringClass: "XXX"'), + array('eq', array(new ToStringClass('XXX'), new ToStringClass('YYY')), 'Expected a value equal to Webmozart\Assert\Tests\ToStringClass: "YYY". Got: Webmozart\Assert\Tests\ToStringClass: "XXX"'), array('eq', array(1, 2), 'Expected a value equal to 2. Got: 1'), array('eq', array(true, false), 'Expected a value equal to false. Got: true'), array('eq', array(true, null), 'Expected a value equal to null. Got: true'), @@ -732,8 +732,8 @@ public function testAnUnknownMethodThrowsABadMethodCall() /** * @ignore */ -class ToStringClass { - +class ToStringClass +{ /** * @var string */ diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index c48b10d0..07a99c7b 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -20,15 +20,17 @@ class BaseTestCase extends TestCase * @param string $exceptionMessage * @param int $exceptionCode */ - public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null) { + public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null) + { if (method_exists($this, 'expectException')) { $this->expectException($exceptionName); - if($exceptionMessage) { + if ($exceptionMessage) { $this->expectExceptionMessage($exceptionMessage); } - if( $exceptionCode) { + if ($exceptionCode) { $this->expectExceptionCode($exceptionCode); } + return; } parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode); diff --git a/tests/ProjectCodeTest.php b/tests/ProjectCodeTest.php index d7f611c7..822f5d69 100644 --- a/tests/ProjectCodeTest.php +++ b/tests/ProjectCodeTest.php @@ -2,9 +2,9 @@ namespace Webmozart\Assert\Tests; -use Webmozart\Assert\Bin\MixinGenerator; use ReflectionClass; use ReflectionMethod; +use Webmozart\Assert\Bin\MixinGenerator; /** * @coversNothing @@ -20,7 +20,7 @@ class ProjectCodeTest extends BaseTestCase */ public static function doSetUpBeforeClass() { - self::$readmeContent = file_get_contents(__DIR__ . '/../README.md'); + self::$readmeContent = file_get_contents(__DIR__.'/../README.md'); $rc = new ReflectionClass('\Webmozart\Assert\Mixin'); self::$assertDocComment = $rc->getDocComment(); @@ -38,13 +38,14 @@ public static function doSetUpBeforeClass() */ public function testHasNullOr($method) { - $fullMethodName = 'nullOr' . ucfirst($method); + $fullMethodName = 'nullOr'.ucfirst($method); if ($method === 'notNull' || $method === 'null') { $this->addToAssertionCount(1); + return; } - $correct = strpos( (string)self::$assertDocComment,'@method static void ' . $fullMethodName); + $correct = strpos((string) self::$assertDocComment, '@method static void '.$fullMethodName); if (!$correct) { $correct = in_array($fullMethodName, self::$mixinMethodNames, true); } @@ -56,7 +57,7 @@ public function testHasNullOr($method) )); } - $this->addToAssertionCount(1); + $this->addToAssertionCount(1); } /** @@ -66,9 +67,9 @@ public function testHasNullOr($method) */ public function testHasAll($method) { - $fullMethodName = 'all' . ucfirst($method); + $fullMethodName = 'all'.ucfirst($method); - $correct = strpos((string) self::$assertDocComment,'@method static void ' . $fullMethodName); + $correct = strpos((string) self::$assertDocComment, '@method static void '.$fullMethodName); if (!$correct) { $correct = in_array($fullMethodName, self::$mixinMethodNames, true); } @@ -90,9 +91,9 @@ public function testHasAll($method) */ public function testIsInReadme($method) { - $correct = strpos((string) self::$readmeContent,$method); + $correct = strpos((string) self::$readmeContent, $method); - if($correct === false) { + if ($correct === false) { $this->fail(sprintf( 'All methods must be documented in the README.md, please add the "%s" method.', $method @@ -126,7 +127,6 @@ public function testHasThrowsAnnotation($method) $method->getName() ) ); - } /** @@ -138,13 +138,14 @@ public function testHasCorrespondingStaticAnalysisFile($method) { $doc = $method->getDocComment(); - if($doc === false || strpos($doc, '@psalm-assert') === false) { + if ($doc === false || strpos($doc, '@psalm-assert') === false) { $this->addToAssertionCount(1); + return; } $this->assertFileExists( - __DIR__ . '/static-analysis/assert-'. $method->getName() . '.php' + __DIR__.'/static-analysis/assert-'.$method->getName().'.php' ); } @@ -152,14 +153,15 @@ public function testMixinIsUpToDateVersion() { if (version_compare(PHP_VERSION, '7.2.0') < 0) { $this->markTestSkipped('mixin generator is implemented using php 7.2 features'); + return; } - require_once __DIR__ . '/../bin/src/MixinGenerator.php'; + require_once __DIR__.'/../bin/src/MixinGenerator.php'; $generator = new MixinGenerator(); - $actual = file_get_contents(__DIR__ . '/../src/Mixin.php'); + $actual = file_get_contents(__DIR__.'/../src/Mixin.php'); $this->assertEquals($generator->generate(), $actual, 'please regenerate Mixin with `php bin/generate.php` command in the project root'); } @@ -169,7 +171,7 @@ public function testMixinIsUpToDateVersion() */ public function providesMethodNames() { - return array_map(function($value) { + return array_map(function ($value) { return array($value->getName()); }, $this->getMethods()); } @@ -179,7 +181,7 @@ public function providesMethodNames() */ public function provideMethods() { - return array_map(function($value) { + return array_map(function ($value) { return array($value); }, $this->getMethods()); } diff --git a/tests/static-analysis/assert-isNotA.php b/tests/static-analysis/assert-isNotA.php index 5aa2c1f9..f8b9ba29 100644 --- a/tests/static-analysis/assert-isNotA.php +++ b/tests/static-analysis/assert-isNotA.php @@ -2,8 +2,8 @@ namespace Webmozart\Assert\Tests\StaticAnalysis; -use stdClass; use DateTime; +use stdClass; use Webmozart\Assert\Assert; /** diff --git a/tests/static-analysis/assert-keyNotExists.php b/tests/static-analysis/assert-keyNotExists.php index 0edc8fcd..de14f1e9 100644 --- a/tests/static-analysis/assert-keyNotExists.php +++ b/tests/static-analysis/assert-keyNotExists.php @@ -32,7 +32,6 @@ function nullOrKeyNotExists(?array $array, $key): ?array * @psalm-pure * * @param iterable $array - * * @param array-key $key * * @return iterable diff --git a/tests/static-analysis/assert-notEmpty.php b/tests/static-analysis/assert-notEmpty.php index 9bdc12fb..83a4ef57 100644 --- a/tests/static-analysis/assert-notEmpty.php +++ b/tests/static-analysis/assert-notEmpty.php @@ -50,7 +50,6 @@ function notEmptyArray(array $value) return $value; } - /** * @psalm-pure * diff --git a/tests/static-analysis/assert-notInstanceOf.php b/tests/static-analysis/assert-notInstanceOf.php index d5a331b7..0236894f 100644 --- a/tests/static-analysis/assert-notInstanceOf.php +++ b/tests/static-analysis/assert-notInstanceOf.php @@ -2,8 +2,8 @@ namespace Webmozart\Assert\Tests\StaticAnalysis; -use stdClass; use DateTime; +use stdClass; use Webmozart\Assert\Assert; /**