Project.php in Coder 8.2
File
coder_sniffer/DrupalPractice/Project.php
View source
<?php
namespace DrupalPractice;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\InfoFiles\ClassFilesSniff;
use Symfony\Component\Yaml\Yaml;
class Project {
public static function getName(File $phpcsFile) {
static $cache;
if (isset($cache[$phpcsFile
->getFilename()]) === true) {
return $cache[$phpcsFile
->getFilename()];
}
$pathParts = pathinfo($phpcsFile
->getFilename());
if (isset($pathParts['extension']) === true && ($pathParts['extension'] === 'module' || $pathParts['extension'] === 'install')) {
$cache[$phpcsFile
->getFilename()] = $pathParts['filename'];
return $pathParts['filename'];
}
$infoFile = static::getInfoFile($phpcsFile);
if ($infoFile === false) {
return false;
}
$pathParts = pathinfo($infoFile);
$cache[$phpcsFile
->getFilename()] = $pathParts['filename'];
return $pathParts['filename'];
}
public static function getInfoFile(File $phpcsFile) {
static $cache;
if (isset($cache[$phpcsFile
->getFilename()]) === true) {
return $cache[$phpcsFile
->getFilename()];
}
$pathParts = pathinfo($phpcsFile
->getFilename());
$dir = $pathParts['dirname'];
do {
$infoFiles = glob("{$dir}/*.info.yml");
if (empty($infoFiles) === true) {
$infoFiles = glob("{$dir}/*.info");
}
$dir = dirname($dir);
} while (empty($infoFiles) === true && $dir !== dirname($dir));
if (empty($infoFiles) === true) {
$cache[$phpcsFile
->getFilename()] = false;
return false;
}
usort($infoFiles, array(
__NAMESPACE__ . '\\Project',
'compareLength',
));
$infoFile = $infoFiles[0];
$cache[$phpcsFile
->getFilename()] = $infoFile;
return $infoFile;
}
public static function getServicesYmlFile(File $phpcsFile) {
static $cache;
if (isset($cache[$phpcsFile
->getFilename()]) === true) {
return $cache[$phpcsFile
->getFilename()];
}
$pathParts = pathinfo($phpcsFile
->getFilename());
$dir = $pathParts['dirname'];
do {
$ymlFiles = glob("{$dir}/*.services.yml");
$dir = dirname($dir);
} while (empty($ymlFiles) === true && $dir !== dirname($dir));
if (empty($ymlFiles) === true) {
$cache[$phpcsFile
->getFilename()] = false;
return false;
}
usort($ymlFiles, array(
__NAMESPACE__ . '\\Project',
'compareLength',
));
$ymlFile = $ymlFiles[0];
$cache[$phpcsFile
->getFilename()] = $ymlFile;
return $ymlFile;
}
public static function isServiceClass(File $phpcsFile, $classPtr) {
static $cache;
if (isset($cache[$phpcsFile
->getFilename()]) === true) {
return $cache[$phpcsFile
->getFilename()];
}
$namespacePtr = $phpcsFile
->findPrevious(T_NAMESPACE, $classPtr - 1);
if ($namespacePtr === false) {
$cache[$phpcsFile
->getFilename()] = false;
return false;
}
$ymlFile = static::getServicesYmlFile($phpcsFile);
if ($ymlFile === false) {
$cache[$phpcsFile
->getFilename()] = false;
return false;
}
$services = Yaml::parse(file_get_contents($ymlFile));
if (isset($services['services']) === false) {
$cache[$phpcsFile
->getFilename()] = false;
return false;
}
$nsEnd = $phpcsFile
->findNext([
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
], $namespacePtr + 1, null, true);
$namespace = trim($phpcsFile
->getTokensAsString($namespacePtr + 1, $nsEnd - $namespacePtr - 1));
$classNameSpaced = ltrim($namespace . '\\' . $phpcsFile
->getDeclarationName($classPtr), '\\');
foreach ($services['services'] as $service) {
if (isset($service['class']) === true && $classNameSpaced === ltrim($service['class'], '\\')) {
$cache[$phpcsFile
->getFilename()] = true;
return true;
}
}
return false;
}
public static function compareLength($a, $b) {
return strlen($a) - strlen($b);
}
public static function getCoreVersion(File $phpcsFile) {
$infoFile = static::getInfoFile($phpcsFile);
if ($infoFile === false) {
return false;
}
$pathParts = pathinfo($infoFile);
if ($pathParts['extension'] === 'info') {
$info_settings = ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile));
if (isset($info_settings['core']) === true) {
return $info_settings['core'];
}
}
else {
return '8.x';
}
}
}
Classes
Name |
Description |
Project |
Helper class to retrieve project information like module/theme name for a file. |