Commit 0c76f8e9 authored by Klaus Purer's avatar Klaus Purer
Browse files

fix(NamespaceDependency): Allow not namespaced dependencies in theme info YAML...

fix(NamespaceDependency): Allow not namespaced dependencies in theme info YAML files (#2857856 by andypost, kdebisschop, tobby, klausi)
parent 92c3c777
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ namespace DrupalPractice\Sniffs\InfoFiles;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

/**
 * Checks that all declared dependencies are namespaced.
@@ -54,6 +56,19 @@ class NamespacedDependencySniff implements Sniff
            return ($phpcsFile->numTokens + 1);
        }

        $contents = file_get_contents($phpcsFile->getFilename());
        try {
            $info = Yaml::parse($contents);
            // Themes are allowed to have not namespaced dependencies, see
            // https://www.drupal.org/project/drupal/issues/474684.
            if (isset($info['type']) === true && $info['type'] === 'theme') {
                return ($phpcsFile->numTokens + 1);
            }
        } catch (ParseException $e) {
            // If the YAML is invalid we ignore this file.
            return ($phpcsFile->numTokens + 1);
        }

        if (preg_match('/^dependencies:/', $tokens[$stackPtr]['content']) === 0) {
            return;
        }
+17 −7
Original line number Diff line number Diff line
@@ -29,15 +29,22 @@ class NamespacedDependencyUnitTest extends CoderSniffUnitTest
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array(int => int)
     */
    public function getWarningList()
    public function getWarningList($testFile=null)
    {
        switch ($testFile) {
        case 'dependencies_test.info.yml':
            return [
                9  => 1,
                11 => 1,
                13 => 1,
            ];
        case 'dependencies_theme.info.yml':
            return [];
        }

    }//end getWarningList()

@@ -51,7 +58,10 @@ class NamespacedDependencyUnitTest extends CoderSniffUnitTest
     */
    protected function getTestFiles($testFileBase)
    {
        return [__DIR__.'/dependencies_test.info.yml'];
        return [
            __DIR__.'/dependencies_test.info.yml',
            __DIR__.'/dependencies_theme.info.yml',
        ];

    }//end getTestFiles()

+12 −0
Original line number Diff line number Diff line
name: MyTheme
type: theme
description: 'Themes are allowed to have not namespaced dependencies, see https://www.drupal.org/project/drupal/issues/474684'
package: Core
version: VERSION
core: 8.x
dependencies:
  - text
  - rest
  - views
  # Some comment here.
  - rules