Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
Showing with 37 additions and 20 deletions
...@@ -38,7 +38,7 @@ class DisallowLongArraySyntaxSniff extends GenericDisallowLongArraySyntaxSniff ...@@ -38,7 +38,7 @@ class DisallowLongArraySyntaxSniff extends GenericDisallowLongArraySyntaxSniff
public function process(File $phpcsFile, $stackPtr) public function process(File $phpcsFile, $stackPtr)
{ {
$drupalVersion = Project::getCoreVersion($phpcsFile); $drupalVersion = Project::getCoreVersion($phpcsFile);
if ($drupalVersion !== '8.x') { if ($drupalVersion < 8) {
// No need to check this file again, mark it as done. // No need to check this file again, mark it as done.
return ($phpcsFile->numTokens + 1); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -12,6 +12,7 @@ namespace DrupalPractice; ...@@ -12,6 +12,7 @@ namespace DrupalPractice;
use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Files\File;
use \Drupal\Sniffs\InfoFiles\ClassFilesSniff; use \Drupal\Sniffs\InfoFiles\ClassFilesSniff;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use PHP_CodeSniffer\Config;
/** /**
* Helper class to retrieve project information like module/theme name for a file. * Helper class to retrieve project information like module/theme name for a file.
...@@ -234,14 +235,21 @@ class Project ...@@ -234,14 +235,21 @@ class Project
* *
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* *
* @return string|false The core version string or false if it could not * @return int The core version number. Returns 8 by default.
* be derived.
*/ */
public static function getCoreVersion(File $phpcsFile) 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); $infoFile = static::getInfoFile($phpcsFile);
if ($infoFile === false) { if ($infoFile === false) {
return false; // Default to Drupal 8.
return 8;
} }
$pathParts = pathinfo($infoFile); $pathParts = pathinfo($infoFile);
...@@ -249,16 +257,21 @@ class Project ...@@ -249,16 +257,21 @@ class Project
// Drupal 6 and 7 use the .info file extension. // Drupal 6 and 7 use the .info file extension.
if ($pathParts['extension'] === 'info') { if ($pathParts['extension'] === 'info') {
$infoSettings = ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile)); $infoSettings = ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile));
if (isset($infoSettings['core']) === true) { if (isset($infoSettings['core']) === true
return $infoSettings['core']; && is_string($infoSettings['core']) === true
) {
return (int) $infoSettings['core']{0};
} }
} else {
// Drupal 8 uses the .yml file extension. // Default to Drupal 7 if there is an info file.
// @todo Revisit for Drupal 9, but I don't want to do YAML parsing return 7;
// for now.
return '8.x';
} }
// 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;
}//end getCoreVersion() }//end getCoreVersion()
......
...@@ -55,7 +55,7 @@ class GlobalConstantSniff implements Sniff ...@@ -55,7 +55,7 @@ class GlobalConstantSniff implements Sniff
} }
$coreVersion = Project::getCoreVersion($phpcsFile); $coreVersion = Project::getCoreVersion($phpcsFile);
if ($coreVersion !== '8.x') { if ($coreVersion < 8) {
// No need to check this file again, mark it as done. // No need to check this file again, mark it as done.
return ($phpcsFile->numTokens + 1); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -63,7 +63,7 @@ class GlobalDefineSniff extends FunctionCall ...@@ -63,7 +63,7 @@ class GlobalDefineSniff extends FunctionCall
} }
$coreVersion = Project::getCoreVersion($phpcsFile); $coreVersion = Project::getCoreVersion($phpcsFile);
if ($coreVersion !== '8.x') { if ($coreVersion < 8) {
// No need to check this file again, mark it as done. // No need to check this file again, mark it as done.
return ($phpcsFile->numTokens + 1); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -56,7 +56,7 @@ class DbQuerySniff extends FunctionCall ...@@ -56,7 +56,7 @@ class DbQuerySniff extends FunctionCall
$closeBracket $closeBracket
) { ) {
// This check only applies to Drupal 7, not Drupal 6. // 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); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -45,7 +45,7 @@ class HookInitCssSniff extends FunctionDefinition ...@@ -45,7 +45,7 @@ class HookInitCssSniff extends FunctionDefinition
} }
// This check only applies to Drupal 7, not Drupal 6. // 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); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -53,7 +53,7 @@ class InstallTSniff extends FunctionDefinition ...@@ -53,7 +53,7 @@ class InstallTSniff extends FunctionDefinition
} }
// This check only applies to Drupal 7, not Drupal 8. // This check only applies to Drupal 7, not Drupal 8.
if (Project::getCoreVersion($phpcsFile) !== '7.x') { if (Project::getCoreVersion($phpcsFile) !== 7) {
return ($phpcsFile->numTokens + 1); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -70,7 +70,7 @@ class GlobalFunctionSniff extends FunctionCall ...@@ -70,7 +70,7 @@ class GlobalFunctionSniff extends FunctionCall
$tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens();
// Only run this sniff on Drupal 8+. // Only run this sniff on Drupal 8+.
if (Project::getCoreVersion($phpcsFile) !== '8.x') { if (Project::getCoreVersion($phpcsFile) < 8) {
// No need to check this file again, mark it as done. // No need to check this file again, mark it as done.
return ($phpcsFile->numTokens + 1); return ($phpcsFile->numTokens + 1);
} }
......
...@@ -96,15 +96,19 @@ class ProjectUnitTest extends \PHPUnit_Framework_TestCase ...@@ -96,15 +96,19 @@ class ProjectUnitTest extends \PHPUnit_Framework_TestCase
return [ return [
[ [
dirname(__FILE__).'/modules/drupal6/nested/test.php', dirname(__FILE__).'/modules/drupal6/nested/test.php',
'6.x', 6,
], ],
[ [
dirname(__FILE__).'/modules/drupal7/test.php', dirname(__FILE__).'/modules/drupal7/test.php',
'7.x', 7,
], ],
[ [
dirname(__FILE__).'/modules/drupal8/test.php', dirname(__FILE__).'/modules/drupal8/test.php',
'8.x', 8,
],
[
'invalid',
8,
], ],
]; ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment