Browse Source

DX: Cover `php-cs-fixer` file with static analysis (#8229)

Co-authored-by: Greg Korba <greg@codito.dev>
Michael Voříšek 4 months ago
parent
commit
5937ce4eaf

+ 1 - 0
.php-cs-fixer.dist.php

@@ -50,5 +50,6 @@ return (new Config())
             ->ignoreVCSIgnored(true)
             ->exclude(['dev-tools/phpstan', 'tests/Fixtures'])
             ->in(__DIR__)
+            ->append([__DIR__.'/php-cs-fixer'])
     )
 ;

+ 17 - 11
php-cs-fixer

@@ -1,6 +1,8 @@
 #!/usr/bin/env php
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -11,28 +13,33 @@
  * with this source code in the file LICENSE.
  */
 
+use Composer\XdebugHandler\XdebugHandler;
+use PhpCsFixer\Console\Application;
+
 error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
 
-set_error_handler(static function ($severity, $message, $file, $line) {
-    if ($severity & error_reporting()) {
+set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
+    if (0 !== ($severity & error_reporting())) {
         throw new ErrorException($message, 0, $severity, $file, $line);
     }
+
+    return true;
 });
 
 // check environment requirements
-(function () {
-    if (\PHP_VERSION_ID === 80000) {
+(static function (): void {
+    if (\PHP_VERSION_ID === (int) '80000') { // TODO use 8_00_00 once only PHP 7.4+ is supported by this entry file
         fwrite(STDERR, "PHP CS Fixer is not able run on PHP 8.0.0 due to bug in PHP tokenizer (https://bugs.php.net/bug.php?id=80462).\n");
         fwrite(STDERR, "Update PHP version to unblock execution.\n");
 
         exit(1);
     }
 
-    if (\PHP_VERSION_ID < 70400 || \PHP_VERSION_ID >= 80400) {
+    if (\PHP_VERSION_ID < (int) '70400' || \PHP_VERSION_ID >= (int) '80400') {
         fwrite(STDERR, "PHP needs to be a minimum version of PHP 7.4.0 and maximum version of PHP 8.3.*.\n");
         fwrite(STDERR, 'Current PHP version: '.PHP_VERSION.".\n");
 
-        if (getenv('PHP_CS_FIXER_IGNORE_ENV')) {
+        if (filter_var(getenv('PHP_CS_FIXER_IGNORE_ENV'), FILTER_VALIDATE_BOOLEAN)) {
             fwrite(STDERR, "Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.\n");
         } else {
             fwrite(STDERR, "To ignore this requirement please set `PHP_CS_FIXER_IGNORE_ENV`.\n");
@@ -47,7 +54,7 @@ set_error_handler(static function ($severity, $message, $file, $line) {
         if (!extension_loaded($extension)) {
             fwrite(STDERR, sprintf("PHP extension ext-%s is missing from your system. Install or enable it.\n", $extension));
 
-            if (getenv('PHP_CS_FIXER_IGNORE_ENV')) {
+            if (filter_var(getenv('PHP_CS_FIXER_IGNORE_ENV'), FILTER_VALIDATE_BOOLEAN)) {
                 fwrite(STDERR, "Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.\n");
             } else {
                 exit(1);
@@ -57,14 +64,16 @@ set_error_handler(static function ($severity, $message, $file, $line) {
 })();
 
 // load dependencies
-(function () {
+(static function (): void {
     $require = true;
     if (class_exists('Phar')) {
         // Maybe this file is used as phar-stub? Let's try!
         try {
             Phar::mapPhar('php-cs-fixer.phar');
 
+            /** @phpstan-ignore requireOnce.fileNotFound */
             require_once 'phar://php-cs-fixer.phar/vendor/autoload.php';
+
             $require = false;
         } catch (PharException $e) {
         }
@@ -90,9 +99,6 @@ set_error_handler(static function ($severity, $message, $file, $line) {
     }
 })();
 
-use Composer\XdebugHandler\XdebugHandler;
-use PhpCsFixer\Console\Application;
-
 // Restart if xdebug is loaded, unless the environment variable PHP_CS_FIXER_ALLOW_XDEBUG is set.
 $xdebug = new XdebugHandler('PHP_CS_FIXER');
 $xdebug->check();

+ 1 - 0
phpstan.dist.neon

@@ -9,6 +9,7 @@ parameters:
     paths:
         - src
         - tests
+        - php-cs-fixer
         - dev-tools/phpstan/src
     excludePaths:
         - tests/Fixtures

+ 3 - 3
tests/AutoReview/ReadmeTest.php → tests/AutoReview/BinEntryFileTest.php

@@ -26,7 +26,7 @@ use PhpCsFixer\Tests\TestCase;
  * @group auto-review
  * @group covers-nothing
  */
-final class ReadmeTest extends TestCase
+final class BinEntryFileTest extends TestCase
 {
     public function testSupportedPhpVersions(): void
     {
@@ -43,8 +43,8 @@ final class ReadmeTest extends TestCase
         $file = null;
 
         self::assertEqualsCanonicalizing([
-            '    if (\PHP_VERSION_ID === 80000) {'."\n",
-            '    if (\PHP_VERSION_ID < 70400 || \PHP_VERSION_ID >= 80400) {'."\n",
+            '    if (\PHP_VERSION_ID === (int) \'80000\') { // TODO use 8_00_00 once only PHP 7.4+ is supported by this entry file'."\n",
+            '    if (\PHP_VERSION_ID < (int) \'70400\' || \PHP_VERSION_ID >= (int) \'80400\') {'."\n",
         ], $phpVersionIdLines, 'Seems supported PHP versions changed in "./php-cs-fixer" - edit the README.md (and this test file) to match them!');
     }
 }

+ 8 - 6
tests/AutoReview/CiConfigurationTest.php

@@ -176,7 +176,7 @@ final class CiConfigurationTest extends TestCase
 
     private function convertPhpVerIdToNiceVer(string $verId): string
     {
-        $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture);
+        $matchResult = Preg::match('/^(?<major>\d{1,2})_?(?<minor>\d{2})_?(?<patch>\d{2})$/', $verId, $capture);
         if (!$matchResult) {
             throw new \LogicException(\sprintf('Can\'t parse version "%s" id.', $verId));
         }
@@ -190,16 +190,17 @@ final class CiConfigurationTest extends TestCase
         $sequence = $tokens->findSequence([
             [T_STRING, 'PHP_VERSION_ID'],
             [T_IS_GREATER_OR_EQUAL],
-            [T_LNUMBER],
+            [T_INT_CAST],
+            [T_CONSTANT_ENCAPSED_STRING],
         ]);
 
         if (null === $sequence) {
             throw new \LogicException("Can't find version - perhaps entry file was modified?");
         }
 
-        $phpVerId = (int) end($sequence)->getContent();
+        $phpVerId = trim(end($sequence)->getContent(), '\'');
 
-        return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100));
+        return $this->convertPhpVerIdToNiceVer((string) ((int) $phpVerId - 100));
     }
 
     private function getMinPhpVersionFromEntryFile(): string
@@ -208,14 +209,15 @@ final class CiConfigurationTest extends TestCase
         $sequence = $tokens->findSequence([
             [T_STRING, 'PHP_VERSION_ID'],
             '<',
-            [T_LNUMBER],
+            [T_INT_CAST],
+            [T_CONSTANT_ENCAPSED_STRING],
         ]);
 
         if (null === $sequence) {
             throw new \LogicException("Can't find version - perhaps entry file was modified?");
         }
 
-        $phpVerId = end($sequence)->getContent();
+        $phpVerId = trim(end($sequence)->getContent(), '\'');
 
         return $this->convertPhpVerIdToNiceVer($phpVerId);
     }