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
Branches
Tags 8.3.5 8.x-3.5
No related merge requests found
Showing with 37 additions and 20 deletions
......@@ -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);
}
......
......@@ -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,16 +257,21 @@ 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 {
// 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';
// 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;
}//end getCoreVersion()
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -45,7 +45,7 @@ class HookInitCssSniff extends FunctionDefinition
}
// 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);
}
......
......@@ -53,7 +53,7 @@ class InstallTSniff extends FunctionDefinition
}
// 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);
}
......
......@@ -70,7 +70,7 @@ class GlobalFunctionSniff extends FunctionCall
$tokens = $phpcsFile->getTokens();
// 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.
return ($phpcsFile->numTokens + 1);
}
......
......@@ -96,15 +96,19 @@ class ProjectUnitTest extends \PHPUnit_Framework_TestCase
return [
[
dirname(__FILE__).'/modules/drupal6/nested/test.php',
'6.x',
6,
],
[
dirname(__FILE__).'/modules/drupal7/test.php',
'7.x',
7,
],
[
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