Browse Source

PsrAutoloadingFixer - do not remove directory structure from the Class name

Kuba Werłos 4 years ago
parent
commit
5d1856ffad

+ 21 - 2
.github/workflows/ci.yml

@@ -34,6 +34,12 @@ jobs:
             PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER: 1
             SYMFONY_DEPRECATIONS_HELPER: 'disabled'
 
+          - operating-system: 'ubuntu-20.04'
+            php-version: '7.4'
+            job-description: 'with calculating code coverage'
+            calculate-code-coverage: 'yes'
+            phpunit-flags: '--testsuite coverage --exclude-group covers-nothing --coverage-clover build/logs/clover.xml'
+
           - operating-system: 'ubuntu-20.04'
             php-version: '7.4'
             job-description: 'with migration rules'
@@ -61,11 +67,18 @@ jobs:
       - name: Checkout code
         uses: actions/checkout@v2
 
+      - name: Get code coverage driver
+        uses: actions/github-script@v3.1
+        id: code-coverage-driver
+        with:
+          script: 'return "${{ matrix.calculate-code-coverage }}" == "yes" ? "pcov" : "none"'
+          result-encoding: string
+
       - name: Setup PHP
         uses: shivammathur/setup-php@v2
         with:
           php-version: ${{ matrix.php-version }}
-          coverage: none
+          coverage: ${{ steps.code-coverage-driver.outputs.result }}
           tools: flex
         env:
           fail-fast: false # disabled as old PHP version cannot run flex
@@ -110,7 +123,13 @@ jobs:
           FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }}
           PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER: ${{ matrix.PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER }}
           SYMFONY_DEPRECATIONS_HELPER: ${{ matrix.SYMFONY_DEPRECATIONS_HELPER }}
-        run: vendor/bin/phpunit
+        run: vendor/bin/phpunit ${{ matrix.phpunit-flags }}
+
+      - name: Upload coverage results to Coveralls
+        if: matrix.calculate-code-coverage == 'yes'
+        env:
+          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        run: php vendor/bin/php-coveralls --verbose
 
       - name: Run PHP CS Fixer
         env:

+ 0 - 27
.travis.yml

@@ -22,33 +22,6 @@ before_install:
 
 jobs:
     include:
-        -
-            stage: Test
-            php: 7.4
-            name: 7.4 | Collect coverage
-            before_install:
-                # for building a tag release we don't need to collect code coverage
-                - if [ $TRAVIS_TAG ]; then travis_terminate 0; fi
-
-                ## regular `before_install`
-                # turn off XDebug
-                - phpenv config-rm xdebug.ini || return 0
-
-                # Require PHPUnit 8
-                - composer require --dev --no-update phpunit/phpunit:^8
-
-                # Install PCOV
-                - pecl install pcov
-            install:
-                - travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
-                - composer info -D | sort
-            before_script:
-                # Make code compatible with PHPUnit 8
-                - PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer fix --rules=void_return -q tests || return 0
-            script:
-                - vendor/bin/phpunit --testsuite coverage --exclude-group covers-nothing --coverage-clover build/logs/clover.xml || travis_terminate 1
-                - php vendor/bin/php-coveralls -v
-
         -
             stage: Deployment
             php: 7.4

+ 1 - 1
composer.json

@@ -36,7 +36,7 @@
         "justinrainbow/json-schema": "^5.0",
         "keradus/cli-executor": "^1.4",
         "mikey179/vfsstream": "^1.6",
-        "php-coveralls/php-coveralls": "^2.4.1",
+        "php-coveralls/php-coveralls": "^2.4.2",
         "php-cs-fixer/accessible-object": "^1.0",
         "php-cs-fixer/phpunit-constraint-isidenticalstring": "^1.2",
         "php-cs-fixer/phpunit-constraint-xmlmatchesxsd": "^1.2.1",

+ 1 - 1
doc/rules/phpdoc/phpdoc_order_by_value.rst

@@ -12,7 +12,7 @@ Configuration
 
 List of annotations to order, e.g. ``["covers"]``.
 
-Allowed values: a subset of ``['author', 'covers', 'coversNothing', 'dataProvider', 'depends', 'group', 'internal', 'requires', 'uses']``
+Allowed values: a subset of ``['author', 'covers', 'coversNothing', 'dataProvider', 'depends', 'group', 'internal', 'requires', 'throws', 'uses']``
 
 Default value: ``['covers']``
 

+ 1 - 0
phpunit.xml.dist

@@ -16,6 +16,7 @@
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     enforceTimeLimit="true"
+    failOnWarning="true"
     processIsolation="false"
     stopOnFailure="false"
     timeoutForSmallTests="2"

+ 68 - 5
src/Fixer/Basic/PsrAutoloadingFixer.php

@@ -172,11 +172,7 @@ class InvalidName {}
             return;
         }
 
-        if (null === $namespace && null !== $this->configuration['dir']) {
-            $expectedClassyName = substr(str_replace('/', '_', $file->getRealPath()), -\strlen($classyName) - 4, -4);
-        } else {
-            $expectedClassyName = $file->getBasename('.php');
-        }
+        $expectedClassyName = $this->calculateClassyName($file, $namespace, $classyName);
 
         if ($classyName !== $expectedClassyName) {
             $tokens[$classyIndex] = new Token([T_STRING, $expectedClassyName]);
@@ -210,4 +206,71 @@ class InvalidName {}
             $tokens->insertAt($namespaceStartIndex, $newNamespace);
         }
     }
+
+    /**
+     * @param null|string $namespace
+     * @param string      $currentName
+     *
+     * @return string
+     */
+    private function calculateClassyName(\SplFileInfo $file, $namespace, $currentName)
+    {
+        $name = $file->getBasename('.php');
+
+        $maxNamespace = $this->calculateMaxNamespace($file, $namespace);
+
+        if (null !== $this->configuration['dir']) {
+            return ('' !== $maxNamespace ? (str_replace('\\', '_', $maxNamespace).'_') : '').$name;
+        }
+
+        $namespaceParts = array_reverse(explode('\\', $maxNamespace));
+
+        foreach ($namespaceParts as $namespacePart) {
+            $nameCandidate = sprintf('%s_%s', $namespacePart, $name);
+            if (strtolower($nameCandidate) !== strtolower(substr($currentName, -\strlen($nameCandidate)))) {
+                break;
+            }
+            $name = $nameCandidate;
+        }
+
+        return $name;
+    }
+
+    /**
+     * @param null|string $namespace
+     *
+     * @return string
+     */
+    private function calculateMaxNamespace(\SplFileInfo $file, $namespace)
+    {
+        if (null === $this->configuration['dir']) {
+            $root = \dirname($file->getRealPath());
+            while ($root !== \dirname($root)) {
+                $root = \dirname($root);
+            }
+        } else {
+            $root = realpath($this->configuration['dir']);
+        }
+
+        $namespaceAccordingToFileLocation = trim(str_replace(\DIRECTORY_SEPARATOR, '\\', substr(\dirname($file->getRealPath()), \strlen($root))), '\\');
+
+        if (null === $namespace) {
+            return $namespaceAccordingToFileLocation;
+        }
+
+        $namespaceAccordingToFileLocationPartsReversed = array_reverse(explode('\\', $namespaceAccordingToFileLocation));
+        $namespacePartsReversed = array_reverse(explode('\\', $namespace));
+
+        foreach ($namespacePartsReversed as $key => $namespaceParte) {
+            if (!isset($namespaceAccordingToFileLocationPartsReversed[$key])) {
+                break;
+            }
+            if (strtolower($namespaceParte) !== strtolower($namespaceAccordingToFileLocationPartsReversed[$key])) {
+                break;
+            }
+            unset($namespaceAccordingToFileLocationPartsReversed[$key]);
+        }
+
+        return implode('\\', array_reverse($namespaceAccordingToFileLocationPartsReversed));
+    }
 }

+ 10 - 0
src/Fixer/Comment/SingleLineCommentStyleFixer.php

@@ -98,6 +98,16 @@ $c = 3;
         );
     }
 
+    /**
+     * {@inheritdoc}
+     *
+     * Must run after NoUselessReturnFixer.
+     */
+    public function getPriority()
+    {
+        return -19;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 2 - 2
src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php

@@ -218,7 +218,7 @@ yield  from  baz();
                 continue;
             }
 
-            if ($token->isGivenKind(T_EXTENDS) && $this->isMultilineExtendsWithMoreThanOneAncestor($tokens, $index)) {
+            if ($token->isGivenKind([T_EXTENDS, T_IMPLEMENTS]) && $this->isMultilineExtendsOrImplementsWithMoreThanOneAncestor($tokens, $index)) {
                 continue;
             }
 
@@ -298,7 +298,7 @@ yield  from  baz();
      *
      * @return bool
      */
-    private function isMultilineExtendsWithMoreThanOneAncestor(Tokens $tokens, $index)
+    private function isMultilineExtendsOrImplementsWithMoreThanOneAncestor(Tokens $tokens, $index)
     {
         $hasMoreThanOneAncestor = false;
 

+ 1 - 1
src/Fixer/Phpdoc/PhpdocNoAccessFixer.php

@@ -49,7 +49,7 @@ class Foo
     /**
      * {@inheritdoc}
      *
-     * Must run before NoEmptyPhpdocFixer, PhpdocAlignFixer, PhpdocOrderFixer, PhpdocSeparationFixer, PhpdocTrimFixer.
+     * Must run before NoEmptyPhpdocFixer, PhpdocAlignFixer, PhpdocSeparationFixer, PhpdocTrimFixer.
      * Must run after CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
      */
     public function getPriority()

+ 1 - 1
src/Fixer/Phpdoc/PhpdocNoPackageFixer.php

@@ -49,7 +49,7 @@ class Baz
     /**
      * {@inheritdoc}
      *
-     * Must run before NoEmptyPhpdocFixer, PhpdocAlignFixer, PhpdocOrderFixer, PhpdocSeparationFixer, PhpdocTrimFixer.
+     * Must run before NoEmptyPhpdocFixer, PhpdocAlignFixer, PhpdocSeparationFixer, PhpdocTrimFixer.
      * Must run after CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
      */
     public function getPriority()

Some files were not shown because too many files changed in this diff