Commit 35277fc8 authored by Klaus Purer's avatar Klaus Purer
Browse files

feat(Project): Improve core version handling by allowing a drupal_core_version...

feat(Project): Improve core version handling by allowing a drupal_core_version config option (#2935144)
parent e13f0580
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ class DisallowLongArraySyntaxSniff extends GenericDisallowLongArraySyntaxSniff
    public function process(File $phpcsFile, $stackPtr)
    {
        $drupalVersion = Project::getCoreVersion($phpcsFile);
        if ($drupalVersion !== '8.x') {
        if ($drupalVersion < 8) {
            // No need to check this file again, mark it as done.
            return ($phpcsFile->numTokens + 1);
        }
+23 −10
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ namespace DrupalPractice;
use PHP_CodeSniffer\Files\File;
use \Drupal\Sniffs\InfoFiles\ClassFilesSniff;
use Symfony\Component\Yaml\Yaml;
use PHP_CodeSniffer\Config;

/**
 * Helper class to retrieve project information like module/theme name for a file.
@@ -234,14 +235,21 @@ class Project
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     *
     * @return string|false The core version string or false if it could not
     *   be derived.
     * @return int The core version number. Returns 8 by default.
     */
    public static function getCoreVersion(File $phpcsFile)
    {
        // First check if a config option was passed.
        $coreVersion = Config::getConfigData('drupal_core_version');
        if (empty($coreVersion) === false) {
            return (int) $coreVersion;
        }

        // TRy to guess the core version from info files in the file path.
        $infoFile = static::getInfoFile($phpcsFile);
        if ($infoFile === false) {
            return false;
            // Default to Drupal 8.
            return 8;
        }

        $pathParts = pathinfo($infoFile);
@@ -249,15 +257,20 @@ class Project
        // Drupal 6 and 7 use the .info file extension.
        if ($pathParts['extension'] === 'info') {
            $infoSettings = ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile));
            if (isset($infoSettings['core']) === true) {
                return $infoSettings['core'];
            if (isset($infoSettings['core']) === true
                && is_string($infoSettings['core']) === true
            ) {
                return (int) $infoSettings['core']{0};
            }
        } else {

            // Default to Drupal 7 if there is an info file.
            return 7;
        }

        // Drupal 8 uses the .yml file extension.
        // @todo Revisit for Drupal 9, but I don't want to do YAML parsing
        // for now.
            return '8.x';
        }
        return 8;

    }//end getCoreVersion()

+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ class GlobalConstantSniff implements Sniff
        }

        $coreVersion = Project::getCoreVersion($phpcsFile);
        if ($coreVersion !== '8.x') {
        if ($coreVersion < 8) {
            // No need to check this file again, mark it as done.
            return ($phpcsFile->numTokens + 1);
        }
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ class GlobalDefineSniff extends FunctionCall
        }

        $coreVersion = Project::getCoreVersion($phpcsFile);
        if ($coreVersion !== '8.x') {
        if ($coreVersion < 8) {
            // No need to check this file again, mark it as done.
            return ($phpcsFile->numTokens + 1);
        }
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ class DbQuerySniff extends FunctionCall
        $closeBracket
    ) {
        // This check only applies to Drupal 7, not Drupal 6.
        if (Project::getCoreVersion($phpcsFile) !== '7.x') {
        if (Project::getCoreVersion($phpcsFile) !== 7) {
            return ($phpcsFile->numTokens + 1);
        }

Loading