Drupal investigation

Project.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * DrupalPractice_Project
  4. *
  5. * @category PHP
  6. * @package PHP_CodeSniffer
  7. * @link http://pear.php.net/package/PHP_CodeSniffer
  8. */
  9. use Symfony\Component\Yaml\Yaml;
  10. /**
  11. * Helper class to retrieve project information like module/theme name for a file.
  12. *
  13. * @category PHP
  14. * @package PHP_CodeSniffer
  15. * @link http://pear.php.net/package/PHP_CodeSniffer
  16. */
  17. class DrupalPractice_Project
  18. {
  19. /**
  20. * Determines the project short name a file might be associated with.
  21. *
  22. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  23. *
  24. * @return string|false Returns the project machine name or false if it could not
  25. * be derived.
  26. */
  27. public static function getName(PHP_CodeSniffer_File $phpcsFile)
  28. {
  29. // Cache the project name per file as this might get called often.
  30. static $cache;
  31. if (isset($cache[$phpcsFile->getFilename()]) === true) {
  32. return $cache[$phpcsFile->getFilename()];
  33. }
  34. $pathParts = pathinfo($phpcsFile->getFilename());
  35. // Module and install files are easy: they contain the project name in the
  36. // file name.
  37. if (isset($pathParts['extension']) === true && ($pathParts['extension'] === 'module' || $pathParts['extension'] === 'install')) {
  38. $cache[$phpcsFile->getFilename()] = $pathParts['filename'];
  39. return $pathParts['filename'];
  40. }
  41. $infoFile = static::getInfoFile($phpcsFile);
  42. if ($infoFile === false) {
  43. return false;
  44. }
  45. $pathParts = pathinfo($infoFile);
  46. $cache[$phpcsFile->getFilename()] = $pathParts['filename'];
  47. return $pathParts['filename'];
  48. }//end getName()
  49. /**
  50. * Determines the info file a file might be associated with.
  51. *
  52. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  53. *
  54. * @return string|false The project info file name or false if it could not
  55. * be derived.
  56. */
  57. public static function getInfoFile(PHP_CodeSniffer_File $phpcsFile)
  58. {
  59. // Cache the project name per file as this might get called often.
  60. static $cache;
  61. if (isset($cache[$phpcsFile->getFilename()]) === true) {
  62. return $cache[$phpcsFile->getFilename()];
  63. }
  64. $pathParts = pathinfo($phpcsFile->getFilename());
  65. // Search for an info file.
  66. $dir = $pathParts['dirname'];
  67. do {
  68. $infoFiles = glob("$dir/*.info.yml");
  69. if (empty($infoFiles) === true) {
  70. $infoFiles = glob("$dir/*.info");
  71. }
  72. // Go one directory up if we do not find an info file here.
  73. $dir = dirname($dir);
  74. } while (empty($infoFiles) === true && $dir !== dirname($dir));
  75. // No info file found, so we give up.
  76. if (empty($infoFiles) === true) {
  77. $cache[$phpcsFile->getFilename()] = false;
  78. return false;
  79. }
  80. // Sort the info file names and take the shortest info file.
  81. usort($infoFiles, array('DrupalPractice_Project', 'compareLength'));
  82. $infoFile = $infoFiles[0];
  83. $cache[$phpcsFile->getFilename()] = $infoFile;
  84. return $infoFile;
  85. }//end getInfoFile()
  86. /**
  87. * Determines the *.services.yml file in a module.
  88. *
  89. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  90. *
  91. * @return string|false The Services YML file name or false if it could not
  92. * be derived.
  93. */
  94. public static function getServicesYmlFile(PHP_CodeSniffer_File $phpcsFile)
  95. {
  96. // Cache the services file per file as this might get called often.
  97. static $cache;
  98. if (isset($cache[$phpcsFile->getFilename()]) === true) {
  99. return $cache[$phpcsFile->getFilename()];
  100. }
  101. $pathParts = pathinfo($phpcsFile->getFilename());
  102. // Search for an info file.
  103. $dir = $pathParts['dirname'];
  104. do {
  105. $ymlFiles = glob("$dir/*.services.yml");
  106. // Go one directory up if we do not find an info file here.
  107. $dir = dirname($dir);
  108. } while (empty($ymlFiles) === true && $dir !== dirname($dir));
  109. // No YML file found, so we give up.
  110. if (empty($ymlFiles) === true) {
  111. $cache[$phpcsFile->getFilename()] = false;
  112. return false;
  113. }
  114. // Sort the YML file names and take the shortest info file.
  115. usort($ymlFiles, array('DrupalPractice_Project', 'compareLength'));
  116. $ymlFile = $ymlFiles[0];
  117. $cache[$phpcsFile->getFilename()] = $ymlFile;
  118. return $ymlFile;
  119. }//end getServicesYmlFile()
  120. /**
  121. * Return true if the given class is a Drupal service registered in *.services.yml.
  122. *
  123. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  124. * @param int $classPtr The position of the class declaration
  125. * in the token stack.
  126. *
  127. * @return bool
  128. */
  129. public static function isServiceClass(PHP_CodeSniffer_File $phpcsFile, $classPtr)
  130. {
  131. // Cache the information per file as this might get called often.
  132. static $cache;
  133. if (isset($cache[$phpcsFile->getFilename()]) === true) {
  134. return $cache[$phpcsFile->getFilename()];
  135. }
  136. // Get the namespace of the class if there is one.
  137. $namespacePtr = $phpcsFile->findPrevious(T_NAMESPACE, ($classPtr - 1));
  138. if ($namespacePtr === false) {
  139. $cache[$phpcsFile->getFilename()] = false;
  140. return false;
  141. }
  142. $ymlFile = static::getServicesYmlFile($phpcsFile);
  143. if ($ymlFile === false) {
  144. $cache[$phpcsFile->getFilename()] = false;
  145. return false;
  146. }
  147. $services = Yaml::parse(file_get_contents($ymlFile));
  148. if (isset($services['services']) === false) {
  149. $cache[$phpcsFile->getFilename()] = false;
  150. return false;
  151. }
  152. $nsEnd = $phpcsFile->findNext(
  153. [
  154. T_NS_SEPARATOR,
  155. T_STRING,
  156. T_WHITESPACE,
  157. ],
  158. ($namespacePtr + 1),
  159. null,
  160. true
  161. );
  162. $namespace = trim($phpcsFile->getTokensAsString(($namespacePtr + 1), ($nsEnd - $namespacePtr - 1)));
  163. $classNameSpaced = ltrim($namespace.'\\'.$phpcsFile->getDeclarationName($classPtr), '\\');
  164. foreach ($services['services'] as $service) {
  165. if (isset($service['class']) === true
  166. && $classNameSpaced === ltrim($service['class'], '\\')
  167. ) {
  168. $cache[$phpcsFile->getFilename()] = true;
  169. return true;
  170. }
  171. }
  172. return false;
  173. }//end isServiceClass()
  174. /**
  175. * Helper method to sort array values by string length with usort().
  176. *
  177. * @param string $a First string.
  178. * @param string $b Second string.
  179. *
  180. * @return int
  181. */
  182. public static function compareLength($a, $b)
  183. {
  184. return (strlen($a) - strlen($b));
  185. }//end compareLength()
  186. /**
  187. * Determines the Drupal core version a file might be associated with.
  188. *
  189. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  190. *
  191. * @return string|false The core version string or false if it could not
  192. * be derived.
  193. */
  194. public static function getCoreVersion(PHP_CodeSniffer_File $phpcsFile)
  195. {
  196. $infoFile = static::getInfoFile($phpcsFile);
  197. if ($infoFile === false) {
  198. return false;
  199. }
  200. $pathParts = pathinfo($infoFile);
  201. // Drupal 6 and 7 use the .info file extension.
  202. if ($pathParts['extension'] === 'info') {
  203. $info_settings = Drupal_Sniffs_InfoFiles_ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile));
  204. if (isset($info_settings['core']) === true) {
  205. return $info_settings['core'];
  206. }
  207. } else {
  208. // Drupal 8 uses the .yml file extension.
  209. // @todo Revisit for Drupal 9, but I don't want to do YAML parsing
  210. // for now.
  211. return '8.x';
  212. }
  213. }//end getCoreVersion()
  214. }//end class