Commit 1e50c14d authored by Mitch Portier's avatar Mitch Portier Committed by Klaus Purer
Browse files

feat(CoreVersionRequirement): Add sniff to check for core_version_requirement...

feat(CoreVersionRequirement): Add sniff to check for core_version_requirement in info.yml files (#3104236 by Arkener)
parent 2fe9c554
Loading
Loading
Loading
Loading
+80 −0
Original line number Diff line number Diff line
<?php

/**
 * \DrupalPractice\Sniffs\InfoFiles\CoreVersionRequirementSniff.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */

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 if the *.info.yml file contains core_version_requirement.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class CoreVersionRequirementSniff implements Sniff
{


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return [T_INLINE_HTML];

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
     * @param int                         $stackPtr  The position of the current token
     *                                               in the stack passed in $tokens.
     *
     * @return int
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        $fileExtension = strtolower(substr($phpcsFile->getFilename(), -9));
        if ($fileExtension !== '.info.yml') {
            return ($phpcsFile->numTokens + 1);
        }

        $contents = file_get_contents($phpcsFile->getFilename());
        try {
            $info = Yaml::parse($contents);
        } catch (ParseException $e) {
            // If the YAML is invalid we ignore this file.
            return ($phpcsFile->numTokens + 1);
        }

        // Test modules can omit the core_version_requirement key.
        if (isset($info['package']) === true && $info['package'] === 'Testing') {
            return ($phpcsFile->numTokens + 1);
        }

        if (isset($info['core_version_requirement']) === false) {
            $warning = '"core_version_requirement" property is missing in the info.yml file';
            $phpcsFile->addWarning($warning, $stackPtr, 'CoreVersionRequirement');
        }

        return ($phpcsFile->numTokens + 1);

    }//end process()


}//end class
+69 −0
Original line number Diff line number Diff line
<?php

namespace DrupalPractice\Test\InfoFiles;

use Drupal\Test\CoderSniffUnitTest;

class CoreVersionRequirementUnitTest extends CoderSniffUnitTest
{


    /**
     * Returns the lines where errors should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array<int, int>
     */
    protected function getErrorList(string $testFile)
    {
        return [];

    }//end getErrorList()


    /**
     * Returns the lines where warnings should occur.
     *
     * 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>
     */
    protected function getWarningList(string $testFile)
    {
        switch ($testFile) {
        case 'core_version.info.yml':
            return [1 => 1];
        case 'core_version_test.info.yml':
            return [];
        }

        return [];

    }//end getWarningList()


    /**
     * Returns a list of test files that should be checked.
     *
     * @param string $testFileBase The base path that the unit tests files will have.
     *
     * @return array The list of test files.
     */
    protected function getTestFiles($testFileBase)
    {
        return [
            __DIR__.'/core_version.info.yml',
            __DIR__.'/core_version_test.info.yml',
        ];

    }//end getTestFiles()


}//end class
+6 −0
Original line number Diff line number Diff line
name: Core version
type: module
description: 'Info files should contain a core_version_requirement key.'
package: Coder
version: VERSION
core: 8.x
+6 −0
Original line number Diff line number Diff line
name: Core version test
type: module
description: 'Test modules can omit the core_version_requirement key.'
package: Testing
version: VERSION
core: 8.x