Drupal investigation

CodeSniffer.php 86KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  1. <?php
  2. /**
  3. * PHP_CodeSniffer tokenizes PHP code and detects violations of a
  4. * defined set of coding standards.
  5. *
  6. * PHP version 5
  7. *
  8. * @category PHP
  9. * @package PHP_CodeSniffer
  10. * @author Greg Sherwood <gsherwood@squiz.net>
  11. * @author Marc McIntyre <mmcintyre@squiz.net>
  12. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  13. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. spl_autoload_register(array('PHP_CodeSniffer', 'autoload'));
  17. if (class_exists('PHP_CodeSniffer_Exception', true) === false) {
  18. throw new Exception('Class PHP_CodeSniffer_Exception not found');
  19. }
  20. if (class_exists('PHP_CodeSniffer_File', true) === false) {
  21. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_File not found');
  22. }
  23. if (class_exists('PHP_CodeSniffer_Fixer', true) === false) {
  24. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Fixer not found');
  25. }
  26. if (class_exists('PHP_CodeSniffer_Tokens', true) === false) {
  27. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Tokens not found');
  28. }
  29. if (class_exists('PHP_CodeSniffer_CLI', true) === false) {
  30. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CLI not found');
  31. }
  32. if (interface_exists('PHP_CodeSniffer_Sniff', true) === false) {
  33. throw new PHP_CodeSniffer_Exception('Interface PHP_CodeSniffer_Sniff not found');
  34. }
  35. /**
  36. * PHP_CodeSniffer tokenizes PHP code and detects violations of a
  37. * defined set of coding standards.
  38. *
  39. * Standards are specified by classes that implement the PHP_CodeSniffer_Sniff
  40. * interface. A sniff registers what token types it wishes to listen for, then
  41. * PHP_CodeSniffer encounters that token, the sniff is invoked and passed
  42. * information about where the token was found in the stack, and the token stack
  43. * itself.
  44. *
  45. * Sniff files and their containing class must be prefixed with Sniff, and
  46. * have an extension of .php.
  47. *
  48. * Multiple PHP_CodeSniffer operations can be performed by re-calling the
  49. * process function with different parameters.
  50. *
  51. * @category PHP
  52. * @package PHP_CodeSniffer
  53. * @author Greg Sherwood <gsherwood@squiz.net>
  54. * @author Marc McIntyre <mmcintyre@squiz.net>
  55. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  56. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  57. * @version Release: @package_version@
  58. * @link http://pear.php.net/package/PHP_CodeSniffer
  59. */
  60. class PHP_CodeSniffer
  61. {
  62. /**
  63. * The current version.
  64. *
  65. * @var string
  66. */
  67. const VERSION = '2.8.1';
  68. /**
  69. * Package stability; either stable, beta or alpha.
  70. *
  71. * @var string
  72. */
  73. const STABILITY = 'stable';
  74. /**
  75. * The file or directory that is currently being processed.
  76. *
  77. * @var string
  78. */
  79. protected $file = '';
  80. /**
  81. * The directories that the processed rulesets are in.
  82. *
  83. * This is declared static because it is also used in the
  84. * autoloader to look for sniffs outside the PHPCS install.
  85. * This way, standards designed to be installed inside PHPCS can
  86. * also be used from outside the PHPCS Standards directory.
  87. *
  88. * @var string
  89. */
  90. protected static $rulesetDirs = array();
  91. /**
  92. * The CLI object controlling the run.
  93. *
  94. * @var PHP_CodeSniffer_CLI
  95. */
  96. public $cli = null;
  97. /**
  98. * The Reporting object controlling report generation.
  99. *
  100. * @var PHP_CodeSniffer_Reporting
  101. */
  102. public $reporting = null;
  103. /**
  104. * An array of sniff objects that are being used to check files.
  105. *
  106. * @var array(PHP_CodeSniffer_Sniff)
  107. */
  108. protected $listeners = array();
  109. /**
  110. * An array of sniffs that are being used to check files.
  111. *
  112. * @var array(string)
  113. */
  114. protected $sniffs = array();
  115. /**
  116. * A mapping of sniff codes to fully qualified class names.
  117. *
  118. * The key is the sniff code and the value
  119. * is the fully qualified name of the sniff class.
  120. *
  121. * @var array<string, string>
  122. */
  123. public $sniffCodes = array();
  124. /**
  125. * The listeners array, indexed by token type.
  126. *
  127. * @var array
  128. */
  129. private $_tokenListeners = array();
  130. /**
  131. * An array of rules from the ruleset.xml file.
  132. *
  133. * It may be empty, indicating that the ruleset does not override
  134. * any of the default sniff settings.
  135. *
  136. * @var array
  137. */
  138. protected $ruleset = array();
  139. /**
  140. * An array of patterns to use for skipping files.
  141. *
  142. * @var array
  143. */
  144. protected $ignorePatterns = array();
  145. /**
  146. * An array of extensions for files we will check.
  147. *
  148. * @var array
  149. */
  150. public $allowedFileExtensions = array();
  151. /**
  152. * An array of default extensions and associated tokenizers.
  153. *
  154. * If no extensions are set, these will be used as the defaults.
  155. * If extensions are set, these will be used when the correct tokenizer
  156. * can not be determined, such as when checking a passed filename instead
  157. * of files in a directory.
  158. *
  159. * @var array
  160. */
  161. public $defaultFileExtensions = array(
  162. 'php' => 'PHP',
  163. 'inc' => 'PHP',
  164. 'js' => 'JS',
  165. 'css' => 'CSS',
  166. );
  167. /**
  168. * An array of variable types for param/var we will check.
  169. *
  170. * @var array(string)
  171. */
  172. public static $allowedTypes = array(
  173. 'array',
  174. 'boolean',
  175. 'float',
  176. 'integer',
  177. 'mixed',
  178. 'object',
  179. 'string',
  180. 'resource',
  181. 'callable',
  182. );
  183. /**
  184. * Constructs a PHP_CodeSniffer object.
  185. *
  186. * @param int $verbosity The verbosity level.
  187. * 1: Print progress information.
  188. * 2: Print tokenizer debug information.
  189. * 3: Print sniff debug information.
  190. * @param int $tabWidth The number of spaces each tab represents.
  191. * If greater than zero, tabs will be replaced
  192. * by spaces before testing each file.
  193. * @param string $encoding The charset of the sniffed files.
  194. * This is important for some reports that output
  195. * with utf-8 encoding as you don't want it double
  196. * encoding messages.
  197. * @param bool $interactive If TRUE, will stop after each file with errors
  198. * and wait for user input.
  199. *
  200. * @see process()
  201. */
  202. public function __construct(
  203. $verbosity=0,
  204. $tabWidth=0,
  205. $encoding='iso-8859-1',
  206. $interactive=false
  207. ) {
  208. if ($verbosity !== null) {
  209. $this->setVerbosity($verbosity);
  210. }
  211. if ($tabWidth !== null) {
  212. $this->setTabWidth($tabWidth);
  213. }
  214. if ($encoding !== null) {
  215. $this->setEncoding($encoding);
  216. }
  217. if ($interactive !== null) {
  218. $this->setInteractive($interactive);
  219. }
  220. if (defined('PHPCS_DEFAULT_ERROR_SEV') === false) {
  221. define('PHPCS_DEFAULT_ERROR_SEV', 5);
  222. }
  223. if (defined('PHPCS_DEFAULT_WARN_SEV') === false) {
  224. define('PHPCS_DEFAULT_WARN_SEV', 5);
  225. }
  226. if (defined('PHP_CODESNIFFER_CBF') === false) {
  227. define('PHP_CODESNIFFER_CBF', false);
  228. }
  229. // Set default CLI object in case someone is running us
  230. // without using the command line script.
  231. $this->cli = new PHP_CodeSniffer_CLI();
  232. $this->cli->errorSeverity = PHPCS_DEFAULT_ERROR_SEV;
  233. $this->cli->warningSeverity = PHPCS_DEFAULT_WARN_SEV;
  234. $this->cli->dieOnUnknownArg = false;
  235. $this->reporting = new PHP_CodeSniffer_Reporting();
  236. }//end __construct()
  237. /**
  238. * Autoload static method for loading classes and interfaces.
  239. *
  240. * @param string $className The name of the class or interface.
  241. *
  242. * @return void
  243. */
  244. public static function autoload($className)
  245. {
  246. if (substr($className, 0, 4) === 'PHP_') {
  247. $newClassName = substr($className, 4);
  248. } else {
  249. $newClassName = $className;
  250. }
  251. $path = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $newClassName).'.php';
  252. if (is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.$path) === true) {
  253. // Check standard file locations based on class name.
  254. include dirname(__FILE__).DIRECTORY_SEPARATOR.$path;
  255. return;
  256. } else {
  257. // Check for included sniffs.
  258. $installedPaths = PHP_CodeSniffer::getInstalledStandardPaths();
  259. foreach ($installedPaths as $installedPath) {
  260. if (is_file($installedPath.DIRECTORY_SEPARATOR.$path) === true) {
  261. include $installedPath.DIRECTORY_SEPARATOR.$path;
  262. return;
  263. }
  264. }
  265. // Check standard file locations based on the loaded rulesets.
  266. foreach (self::$rulesetDirs as $rulesetDir) {
  267. if (is_file(dirname($rulesetDir).DIRECTORY_SEPARATOR.$path) === true) {
  268. include_once dirname($rulesetDir).DIRECTORY_SEPARATOR.$path;
  269. return;
  270. }
  271. }
  272. }//end if
  273. // Everything else.
  274. @include $path;
  275. }//end autoload()
  276. /**
  277. * Sets the verbosity level.
  278. *
  279. * @param int $verbosity The verbosity level.
  280. * 1: Print progress information.
  281. * 2: Print tokenizer debug information.
  282. * 3: Print sniff debug information.
  283. *
  284. * @return void
  285. */
  286. public function setVerbosity($verbosity)
  287. {
  288. if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
  289. define('PHP_CODESNIFFER_VERBOSITY', $verbosity);
  290. }
  291. }//end setVerbosity()
  292. /**
  293. * Sets the tab width.
  294. *
  295. * @param int $tabWidth The number of spaces each tab represents.
  296. * If greater than zero, tabs will be replaced
  297. * by spaces before testing each file.
  298. *
  299. * @return void
  300. */
  301. public function setTabWidth($tabWidth)
  302. {
  303. if (defined('PHP_CODESNIFFER_TAB_WIDTH') === false) {
  304. define('PHP_CODESNIFFER_TAB_WIDTH', $tabWidth);
  305. }
  306. }//end setTabWidth()
  307. /**
  308. * Sets the encoding.
  309. *
  310. * @param string $encoding The charset of the sniffed files.
  311. * This is important for some reports that output
  312. * with utf-8 encoding as you don't want it double
  313. * encoding messages.
  314. *
  315. * @return void
  316. */
  317. public function setEncoding($encoding)
  318. {
  319. if (defined('PHP_CODESNIFFER_ENCODING') === false) {
  320. define('PHP_CODESNIFFER_ENCODING', $encoding);
  321. }
  322. }//end setEncoding()
  323. /**
  324. * Sets the interactive flag.
  325. *
  326. * @param bool $interactive If TRUE, will stop after each file with errors
  327. * and wait for user input.
  328. *
  329. * @return void
  330. */
  331. public function setInteractive($interactive)
  332. {
  333. if (defined('PHP_CODESNIFFER_INTERACTIVE') === false) {
  334. define('PHP_CODESNIFFER_INTERACTIVE', $interactive);
  335. }
  336. }//end setInteractive()
  337. /**
  338. * Sets an array of file extensions that we will allow checking of.
  339. *
  340. * If the extension is one of the defaults, a specific tokenizer
  341. * will be used. Otherwise, the PHP tokenizer will be used for
  342. * all extensions passed.
  343. *
  344. * @param array $extensions An array of file extensions.
  345. *
  346. * @return void
  347. */
  348. public function setAllowedFileExtensions(array $extensions)
  349. {
  350. $newExtensions = array();
  351. foreach ($extensions as $ext) {
  352. $slash = strpos($ext, '/');
  353. if ($slash !== false) {
  354. // They specified the tokenizer too.
  355. list($ext, $tokenizer) = explode('/', $ext);
  356. $newExtensions[$ext] = strtoupper($tokenizer);
  357. continue;
  358. }
  359. if (isset($this->allowedFileExtensions[$ext]) === true) {
  360. $newExtensions[$ext] = $this->allowedFileExtensions[$ext];
  361. } else if (isset($this->defaultFileExtensions[$ext]) === true) {
  362. $newExtensions[$ext] = $this->defaultFileExtensions[$ext];
  363. } else {
  364. $newExtensions[$ext] = 'PHP';
  365. }
  366. }
  367. $this->allowedFileExtensions = $newExtensions;
  368. }//end setAllowedFileExtensions()
  369. /**
  370. * Sets an array of ignore patterns that we use to skip files and folders.
  371. *
  372. * Patterns are not case sensitive.
  373. *
  374. * @param array $patterns An array of ignore patterns. The pattern is the key
  375. * and the value is either "absolute" or "relative",
  376. * depending on how the pattern should be applied to a
  377. * file path.
  378. *
  379. * @return void
  380. */
  381. public function setIgnorePatterns(array $patterns)
  382. {
  383. $this->ignorePatterns = $patterns;
  384. }//end setIgnorePatterns()
  385. /**
  386. * Gets the array of ignore patterns.
  387. *
  388. * Optionally takes a listener to get ignore patterns specified
  389. * for that sniff only.
  390. *
  391. * @param string $listener The listener to get patterns for. If NULL, all
  392. * patterns are returned.
  393. *
  394. * @return array
  395. */
  396. public function getIgnorePatterns($listener=null)
  397. {
  398. if ($listener === null) {
  399. return $this->ignorePatterns;
  400. }
  401. if (isset($this->ignorePatterns[$listener]) === true) {
  402. return $this->ignorePatterns[$listener];
  403. }
  404. return array();
  405. }//end getIgnorePatterns()
  406. /**
  407. * Sets the internal CLI object.
  408. *
  409. * @param object $cli The CLI object controlling the run.
  410. *
  411. * @return void
  412. */
  413. public function setCli($cli)
  414. {
  415. $this->cli = $cli;
  416. }//end setCli()
  417. /**
  418. * Start a PHP_CodeSniffer run.
  419. *
  420. * @param string|array $files The files and directories to process. For
  421. * directories, each sub directory will also
  422. * be traversed for source files.
  423. * @param string|array $standards The set of code sniffs we are testing
  424. * against.
  425. * @param array $restrictions The sniff codes to restrict the
  426. * violations to.
  427. * @param boolean $local If true, don't recurse into directories.
  428. *
  429. * @return void
  430. */
  431. public function process($files, $standards, array $restrictions=array(), $local=false)
  432. {
  433. $files = (array) $files;
  434. $this->initStandard($standards, $restrictions);
  435. $this->processFiles($files, $local);
  436. }//end process()
  437. /**
  438. * Initialise the standard that the run will use.
  439. *
  440. * @param string|array $standards The set of code sniffs we are testing
  441. * against.
  442. * @param array $restrictions The sniff codes to restrict the testing to.
  443. * @param array $exclusions The sniff codes to exclude from testing.
  444. *
  445. * @return void
  446. */
  447. public function initStandard($standards, array $restrictions=array(), array $exclusions=array())
  448. {
  449. $standards = (array) $standards;
  450. // Reset the members.
  451. $this->listeners = array();
  452. $this->sniffs = array();
  453. $this->ruleset = array();
  454. $this->_tokenListeners = array();
  455. self::$rulesetDirs = array();
  456. // Ensure this option is enabled or else line endings will not always
  457. // be detected properly for files created on a Mac with the /r line ending.
  458. ini_set('auto_detect_line_endings', true);
  459. if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) {
  460. // Should be one standard and one sniff being tested at a time.
  461. $installed = $this->getInstalledStandardPath($standards[0]);
  462. if ($installed !== null) {
  463. $standard = $installed;
  464. } else {
  465. $standard = self::realpath($standards[0]);
  466. if (is_dir($standard) === true
  467. && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
  468. ) {
  469. $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
  470. }
  471. }
  472. $sniffs = $this->_expandRulesetReference($restrictions[0], dirname($standard));
  473. } else {
  474. $sniffs = array();
  475. foreach ($standards as $standard) {
  476. $installed = $this->getInstalledStandardPath($standard);
  477. if ($installed !== null) {
  478. $standard = $installed;
  479. } else {
  480. $standard = self::realpath($standard);
  481. if (is_dir($standard) === true
  482. && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
  483. ) {
  484. $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
  485. }
  486. }
  487. if (PHP_CODESNIFFER_VERBOSITY === 1) {
  488. $ruleset = simplexml_load_string(file_get_contents($standard));
  489. if ($ruleset !== false) {
  490. $standardName = (string) $ruleset['name'];
  491. }
  492. echo "Registering sniffs in the $standardName standard... ";
  493. if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
  494. echo PHP_EOL;
  495. }
  496. }
  497. $sniffs = array_merge($sniffs, $this->processRuleset($standard));
  498. }//end foreach
  499. }//end if
  500. $sniffRestrictions = array();
  501. foreach ($restrictions as $sniffCode) {
  502. $parts = explode('.', strtolower($sniffCode));
  503. $sniffRestrictions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff';
  504. }
  505. $sniffExclusions = array();
  506. foreach ($exclusions as $sniffCode) {
  507. $parts = explode('.', strtolower($sniffCode));
  508. $sniffExclusions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff';
  509. }
  510. $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions);
  511. $this->populateTokenListeners();
  512. if (PHP_CODESNIFFER_VERBOSITY === 1) {
  513. $numSniffs = count($this->sniffs);
  514. echo "DONE ($numSniffs sniffs registered)".PHP_EOL;
  515. }
  516. }//end initStandard()
  517. /**
  518. * Processes the files/directories that PHP_CodeSniffer was constructed with.
  519. *
  520. * @param string|array $files The files and directories to process. For
  521. * directories, each sub directory will also
  522. * be traversed for source files.
  523. * @param boolean $local If true, don't recurse into directories.
  524. *
  525. * @return void
  526. * @throws PHP_CodeSniffer_Exception If files are invalid.
  527. */
  528. public function processFiles($files, $local=false)
  529. {
  530. $files = (array) $files;
  531. $cliValues = $this->cli->getCommandLineValues();
  532. $showProgress = $cliValues['showProgress'];
  533. $useColors = $cliValues['colors'];
  534. if (PHP_CODESNIFFER_VERBOSITY > 0) {
  535. echo 'Creating file list... ';
  536. }
  537. if (empty($this->allowedFileExtensions) === true) {
  538. $this->allowedFileExtensions = $this->defaultFileExtensions;
  539. }
  540. $todo = $this->getFilesToProcess($files, $local);
  541. $numFiles = count($todo);
  542. if (PHP_CODESNIFFER_VERBOSITY > 0) {
  543. echo "DONE ($numFiles files in queue)".PHP_EOL;
  544. }
  545. $numProcessed = 0;
  546. $dots = 0;
  547. $maxLength = strlen($numFiles);
  548. $lastDir = '';
  549. foreach ($todo as $file) {
  550. $this->file = $file;
  551. $currDir = dirname($file);
  552. if ($lastDir !== $currDir) {
  553. if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true) {
  554. echo 'Changing into directory '.$currDir.PHP_EOL;
  555. }
  556. $lastDir = $currDir;
  557. }
  558. $phpcsFile = $this->processFile($file, null);
  559. $numProcessed++;
  560. if (PHP_CODESNIFFER_VERBOSITY > 0
  561. || PHP_CODESNIFFER_INTERACTIVE === true
  562. || $showProgress === false
  563. ) {
  564. continue;
  565. }
  566. // Show progress information.
  567. if ($phpcsFile === null) {
  568. echo 'S';
  569. } else {
  570. $errors = $phpcsFile->getErrorCount();
  571. $warnings = $phpcsFile->getWarningCount();
  572. if ($errors > 0) {
  573. if ($useColors === true) {
  574. echo "\033[31m";
  575. }
  576. echo 'E';
  577. } else if ($warnings > 0) {
  578. if ($useColors === true) {
  579. echo "\033[33m";
  580. }
  581. echo 'W';
  582. } else {
  583. echo '.';
  584. }
  585. if ($useColors === true) {
  586. echo "\033[0m";
  587. }
  588. }//end if
  589. $dots++;
  590. if ($dots === 60) {
  591. $padding = ($maxLength - strlen($numProcessed));
  592. echo str_repeat(' ', $padding);
  593. $percent = round(($numProcessed / $numFiles) * 100);
  594. echo " $numProcessed / $numFiles ($percent%)".PHP_EOL;
  595. $dots = 0;
  596. }
  597. }//end foreach
  598. if (PHP_CODESNIFFER_VERBOSITY === 0
  599. && PHP_CODESNIFFER_INTERACTIVE === false
  600. && $showProgress === true
  601. ) {
  602. echo PHP_EOL.PHP_EOL;
  603. }
  604. }//end processFiles()
  605. /**
  606. * Processes a single ruleset and returns a list of the sniffs it represents.
  607. *
  608. * Rules founds within the ruleset are processed immediately, but sniff classes
  609. * are not registered by this method.
  610. *
  611. * @param string $rulesetPath The path to a ruleset XML file.
  612. * @param int $depth How many nested processing steps we are in. This
  613. * is only used for debug output.
  614. *
  615. * @return array
  616. * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid.
  617. */
  618. public function processRuleset($rulesetPath, $depth=0)
  619. {
  620. $rulesetPath = self::realpath($rulesetPath);
  621. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  622. echo str_repeat("\t", $depth);
  623. echo "Processing ruleset $rulesetPath".PHP_EOL;
  624. }
  625. $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
  626. if ($ruleset === false) {
  627. throw new PHP_CodeSniffer_Exception("Ruleset $rulesetPath is not valid");
  628. }
  629. $ownSniffs = array();
  630. $includedSniffs = array();
  631. $excludedSniffs = array();
  632. $cliValues = $this->cli->getCommandLineValues();
  633. $rulesetDir = dirname($rulesetPath);
  634. self::$rulesetDirs[] = $rulesetDir;
  635. if (is_dir($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs') === true) {
  636. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  637. echo str_repeat("\t", $depth);
  638. echo "\tAdding sniff files from \"/.../".basename($rulesetDir)."/Sniffs/\" directory".PHP_EOL;
  639. }
  640. $ownSniffs = $this->_expandSniffDirectory($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs', $depth);
  641. }
  642. // Process custom sniff config settings.
  643. foreach ($ruleset->{'config'} as $config) {
  644. if ($this->_shouldProcessElement($config) === false) {
  645. continue;
  646. }
  647. $this->setConfigData((string) $config['name'], (string) $config['value'], true);
  648. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  649. echo str_repeat("\t", $depth);
  650. echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL;
  651. }
  652. }
  653. foreach ($ruleset->rule as $rule) {
  654. if (isset($rule['ref']) === false
  655. || $this->_shouldProcessElement($rule) === false
  656. ) {
  657. continue;
  658. }
  659. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  660. echo str_repeat("\t", $depth);
  661. echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL;
  662. }
  663. $includedSniffs = array_merge(
  664. $includedSniffs,
  665. $this->_expandRulesetReference($rule['ref'], $rulesetDir, $depth)
  666. );
  667. if (isset($rule->exclude) === true) {
  668. foreach ($rule->exclude as $exclude) {
  669. if ($this->_shouldProcessElement($exclude) === false) {
  670. continue;
  671. }
  672. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  673. echo str_repeat("\t", $depth);
  674. echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL;
  675. }
  676. // Check if a single code is being excluded, which is a shortcut
  677. // for setting the severity of the message to 0.
  678. $parts = explode('.', $exclude['name']);
  679. if (count($parts) === 4) {
  680. $this->ruleset[(string) $exclude['name']]['severity'] = 0;
  681. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  682. echo str_repeat("\t", $depth);
  683. echo "\t\t=> severity set to 0".PHP_EOL;
  684. }
  685. } else {
  686. $excludedSniffs = array_merge(
  687. $excludedSniffs,
  688. $this->_expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1))
  689. );
  690. }
  691. }//end foreach
  692. }//end if
  693. $this->_processRule($rule, $depth);
  694. }//end foreach
  695. // Process custom command line arguments.
  696. $cliArgs = array();
  697. foreach ($ruleset->{'arg'} as $arg) {
  698. if ($this->_shouldProcessElement($arg) === false) {
  699. continue;
  700. }
  701. if (isset($arg['name']) === true) {
  702. $argString = '--'.(string) $arg['name'];
  703. if (isset($arg['value']) === true) {
  704. $argString .= '='.(string) $arg['value'];
  705. }
  706. } else {
  707. $argString = '-'.(string) $arg['value'];
  708. }
  709. $cliArgs[] = $argString;
  710. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  711. echo str_repeat("\t", $depth);
  712. echo "\t=> set command line value $argString".PHP_EOL;
  713. }
  714. }//end foreach
  715. // Set custom php ini values as CLI args.
  716. foreach ($ruleset->{'ini'} as $arg) {
  717. if ($this->_shouldProcessElement($arg) === false) {
  718. continue;
  719. }
  720. if (isset($arg['name']) === false) {
  721. continue;
  722. }
  723. $name = (string) $arg['name'];
  724. $argString = $name;
  725. if (isset($arg['value']) === true) {
  726. $value = (string) $arg['value'];
  727. $argString .= "=$value";
  728. } else {
  729. $value = 'true';
  730. }
  731. $cliArgs[] = '-d';
  732. $cliArgs[] = $argString;
  733. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  734. echo str_repeat("\t", $depth);
  735. echo "\t=> set PHP ini value $name to $value".PHP_EOL;
  736. }
  737. }//end foreach
  738. if (empty($cliValues['files']) === true && $cliValues['stdin'] === null) {
  739. // Process hard-coded file paths.
  740. foreach ($ruleset->{'file'} as $file) {
  741. $file = (string) $file;
  742. $cliArgs[] = $file;
  743. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  744. echo str_repeat("\t", $depth);
  745. echo "\t=> added \"$file\" to the file list".PHP_EOL;
  746. }
  747. }
  748. }
  749. if (empty($cliArgs) === false) {
  750. // Change the directory so all relative paths are worked
  751. // out based on the location of the ruleset instead of
  752. // the location of the user.
  753. $inPhar = self::isPharFile($rulesetDir);
  754. if ($inPhar === false) {
  755. $currentDir = getcwd();
  756. chdir($rulesetDir);
  757. }
  758. $this->cli->setCommandLineValues($cliArgs);
  759. if ($inPhar === false) {
  760. chdir($currentDir);
  761. }
  762. }
  763. // Process custom ignore pattern rules.
  764. foreach ($ruleset->{'exclude-pattern'} as $pattern) {
  765. if ($this->_shouldProcessElement($pattern) === false) {
  766. continue;
  767. }
  768. if (isset($pattern['type']) === false) {
  769. $pattern['type'] = 'absolute';
  770. }
  771. $this->ignorePatterns[(string) $pattern] = (string) $pattern['type'];
  772. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  773. echo str_repeat("\t", $depth);
  774. echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL;
  775. }
  776. }
  777. $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs));
  778. $excludedSniffs = array_unique($excludedSniffs);
  779. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  780. $included = count($includedSniffs);
  781. $excluded = count($excludedSniffs);
  782. echo str_repeat("\t", $depth);
  783. echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL;
  784. }
  785. // Merge our own sniff list with our externally included
  786. // sniff list, but filter out any excluded sniffs.
  787. $files = array();
  788. foreach ($includedSniffs as $sniff) {
  789. if (in_array($sniff, $excludedSniffs) === true) {
  790. continue;
  791. } else {
  792. $files[] = self::realpath($sniff);
  793. }
  794. }
  795. return $files;
  796. }//end processRuleset()
  797. /**
  798. * Expands a directory into a list of sniff files within.
  799. *
  800. * @param string $directory The path to a directory.
  801. * @param int $depth How many nested processing steps we are in. This
  802. * is only used for debug output.
  803. *
  804. * @return array
  805. */
  806. private function _expandSniffDirectory($directory, $depth=0)
  807. {
  808. $sniffs = array();
  809. if (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') === true) {
  810. $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
  811. } else {
  812. $rdi = new RecursiveDirectoryIterator($directory);
  813. }
  814. $di = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD);
  815. $dirLen = strlen($directory);
  816. foreach ($di as $file) {
  817. $filename = $file->getFilename();
  818. // Skip hidden files.
  819. if (substr($filename, 0, 1) === '.') {
  820. continue;
  821. }
  822. // We are only interested in PHP and sniff files.
  823. $fileParts = explode('.', $filename);
  824. if (array_pop($fileParts) !== 'php') {
  825. continue;
  826. }
  827. $basename = basename($filename, '.php');
  828. if (substr($basename, -5) !== 'Sniff') {
  829. continue;
  830. }
  831. $path = $file->getPathname();
  832. // Skip files in hidden directories within the Sniffs directory of this
  833. // standard. We use the offset with strpos() to allow hidden directories
  834. // before, valid example:
  835. // /home/foo/.composer/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/...
  836. if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) {
  837. continue;
  838. }
  839. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  840. echo str_repeat("\t", $depth);
  841. echo "\t\t=> $path".PHP_EOL;
  842. }
  843. $sniffs[] = $path;
  844. }//end foreach
  845. return $sniffs;
  846. }//end _expandSniffDirectory()
  847. /**
  848. * Expands a ruleset reference into a list of sniff files.
  849. *
  850. * @param string $ref The reference from the ruleset XML file.
  851. * @param string $rulesetDir The directory of the ruleset XML file, used to
  852. * evaluate relative paths.
  853. * @param int $depth How many nested processing steps we are in. This
  854. * is only used for debug output.
  855. *
  856. * @return array
  857. * @throws PHP_CodeSniffer_Exception If the reference is invalid.
  858. */
  859. private function _expandRulesetReference($ref, $rulesetDir, $depth=0)
  860. {
  861. // Ignore internal sniffs codes as they are used to only
  862. // hide and change internal messages.
  863. if (substr($ref, 0, 9) === 'Internal.') {
  864. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  865. echo str_repeat("\t", $depth);
  866. echo "\t\t* ignoring internal sniff code *".PHP_EOL;
  867. }
  868. return array();
  869. }
  870. // As sniffs can't begin with a full stop, assume references in
  871. // this format are relative paths and attempt to convert them
  872. // to absolute paths. If this fails, let the reference run through
  873. // the normal checks and have it fail as normal.
  874. if (substr($ref, 0, 1) === '.') {
  875. $realpath = self::realpath($rulesetDir.'/'.$ref);
  876. if ($realpath !== false) {
  877. $ref = $realpath;
  878. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  879. echo str_repeat("\t", $depth);
  880. echo "\t\t=> $ref".PHP_EOL;
  881. }
  882. }
  883. }
  884. // As sniffs can't begin with a tilde, assume references in
  885. // this format at relative to the user's home directory.
  886. if (substr($ref, 0, 2) === '~/') {
  887. $realpath = self::realpath($ref);
  888. if ($realpath !== false) {
  889. $ref = $realpath;
  890. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  891. echo str_repeat("\t", $depth);
  892. echo "\t\t=> $ref".PHP_EOL;
  893. }
  894. }
  895. }
  896. if (is_file($ref) === true) {
  897. if (substr($ref, -9) === 'Sniff.php') {
  898. // A single external sniff.
  899. self::$rulesetDirs[] = dirname(dirname(dirname($ref)));
  900. return array($ref);
  901. }
  902. } else {
  903. // See if this is a whole standard being referenced.
  904. $path = $this->getInstalledStandardPath($ref);
  905. if (self::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) {
  906. // If the ruleset exists inside the phar file, use it.
  907. if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
  908. $path = $path.DIRECTORY_SEPARATOR.'ruleset.xml';
  909. } else {
  910. $path = null;
  911. }
  912. }
  913. if ($path !== null) {
  914. $ref = $path;
  915. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  916. echo str_repeat("\t", $depth);
  917. echo "\t\t=> $ref".PHP_EOL;
  918. }
  919. } else if (is_dir($ref) === false) {
  920. // Work out the sniff path.
  921. $sepPos = strpos($ref, DIRECTORY_SEPARATOR);
  922. if ($sepPos !== false) {
  923. $stdName = substr($ref, 0, $sepPos);
  924. $path = substr($ref, $sepPos);
  925. } else {
  926. $parts = explode('.', $ref);
  927. $stdName = $parts[0];
  928. if (count($parts) === 1) {
  929. // A whole standard?
  930. $path = '';
  931. } else if (count($parts) === 2) {
  932. // A directory of sniffs?
  933. $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1];
  934. } else {
  935. // A single sniff?
  936. $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php';
  937. }
  938. }
  939. $newRef = false;
  940. $stdPath = $this->getInstalledStandardPath($stdName);
  941. if ($stdPath !== null && $path !== '') {
  942. if (self::isPharFile($stdPath) === true
  943. && strpos($stdPath, 'ruleset.xml') === false
  944. ) {
  945. // Phar files can only return the directory,
  946. // since ruleset can be omitted if building one standard.
  947. $newRef = self::realpath($stdPath.$path);
  948. } else {
  949. $newRef = self::realpath(dirname($stdPath).$path);
  950. }
  951. }
  952. if ($newRef === false) {
  953. // The sniff is not locally installed, so check if it is being
  954. // referenced as a remote sniff outside the install. We do this
  955. // by looking through all directories where we have found ruleset
  956. // files before, looking for ones for this particular standard,
  957. // and seeing if it is in there.
  958. foreach (self::$rulesetDirs as $dir) {
  959. if (strtolower(basename($dir)) !== strtolower($stdName)) {
  960. continue;
  961. }
  962. $newRef = self::realpath($dir.$path);
  963. if ($newRef !== false) {
  964. $ref = $newRef;
  965. }
  966. }
  967. } else {
  968. $ref = $newRef;
  969. }
  970. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  971. echo str_repeat("\t", $depth);
  972. echo "\t\t=> $ref".PHP_EOL;
  973. }
  974. }//end if
  975. }//end if
  976. if (is_dir($ref) === true) {
  977. if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
  978. // We are referencing an external coding standard.
  979. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  980. echo str_repeat("\t", $depth);
  981. echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL;
  982. }
  983. return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2));
  984. } else {
  985. // We are referencing a whole directory of sniffs.
  986. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  987. echo str_repeat("\t", $depth);
  988. echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL;
  989. echo str_repeat("\t", $depth);
  990. echo "\t\tAdding sniff files from directory".PHP_EOL;
  991. }
  992. return $this->_expandSniffDirectory($ref, ($depth + 1));
  993. }
  994. } else {
  995. if (is_file($ref) === false) {
  996. $error = "Referenced sniff \"$ref\" does not exist";
  997. throw new PHP_CodeSniffer_Exception($error);
  998. }
  999. if (substr($ref, -9) === 'Sniff.php') {
  1000. // A single sniff.
  1001. return array($ref);
  1002. } else {
  1003. // Assume an external ruleset.xml file.
  1004. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1005. echo str_repeat("\t", $depth);
  1006. echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL;
  1007. }
  1008. return $this->processRuleset($ref, ($depth + 2));
  1009. }
  1010. }//end if
  1011. }//end _expandRulesetReference()
  1012. /**
  1013. * Processes a rule from a ruleset XML file, overriding built-in defaults.
  1014. *
  1015. * @param SimpleXMLElement $rule The rule object from a ruleset XML file.
  1016. * @param int $depth How many nested processing steps we are in.
  1017. * This is only used for debug output.
  1018. *
  1019. * @return void
  1020. */
  1021. private function _processRule($rule, $depth=0)
  1022. {
  1023. $code = (string) $rule['ref'];
  1024. // Custom severity.
  1025. if (isset($rule->severity) === true
  1026. && $this->_shouldProcessElement($rule->severity) === true
  1027. ) {
  1028. if (isset($this->ruleset[$code]) === false) {
  1029. $this->ruleset[$code] = array();
  1030. }
  1031. $this->ruleset[$code]['severity'] = (int) $rule->severity;
  1032. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1033. echo str_repeat("\t", $depth);
  1034. echo "\t\t=> severity set to ".(int) $rule->severity.PHP_EOL;
  1035. }
  1036. }
  1037. // Custom message type.
  1038. if (isset($rule->type) === true
  1039. && $this->_shouldProcessElement($rule->type) === true
  1040. ) {
  1041. if (isset($this->ruleset[$code]) === false) {
  1042. $this->ruleset[$code] = array();
  1043. }
  1044. $this->ruleset[$code]['type'] = (string) $rule->type;
  1045. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1046. echo str_repeat("\t", $depth);
  1047. echo "\t\t=> message type set to ".(string) $rule->type.PHP_EOL;
  1048. }
  1049. }
  1050. // Custom message.
  1051. if (isset($rule->message) === true
  1052. && $this->_shouldProcessElement($rule->message) === true
  1053. ) {
  1054. if (isset($this->ruleset[$code]) === false) {
  1055. $this->ruleset[$code] = array();
  1056. }
  1057. $this->ruleset[$code]['message'] = (string) $rule->message;
  1058. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1059. echo str_repeat("\t", $depth);
  1060. echo "\t\t=> message set to ".(string) $rule->message.PHP_EOL;
  1061. }
  1062. }
  1063. // Custom properties.
  1064. if (isset($rule->properties) === true
  1065. && $this->_shouldProcessElement($rule->properties) === true
  1066. ) {
  1067. foreach ($rule->properties->property as $prop) {
  1068. if ($this->_shouldProcessElement($prop) === false) {
  1069. continue;
  1070. }
  1071. if (isset($this->ruleset[$code]) === false) {
  1072. $this->ruleset[$code] = array(
  1073. 'properties' => array(),
  1074. );
  1075. } else if (isset($this->ruleset[$code]['properties']) === false) {
  1076. $this->ruleset[$code]['properties'] = array();
  1077. }
  1078. $name = (string) $prop['name'];
  1079. if (isset($prop['type']) === true
  1080. && (string) $prop['type'] === 'array'
  1081. ) {
  1082. $value = (string) $prop['value'];
  1083. $values = array();
  1084. foreach (explode(',', $value) as $val) {
  1085. $v = '';
  1086. list($k,$v) = explode('=>', $val.'=>');
  1087. if ($v !== '') {
  1088. $values[$k] = $v;
  1089. } else {
  1090. $values[] = $k;
  1091. }
  1092. }
  1093. $this->ruleset[$code]['properties'][$name] = $values;
  1094. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1095. echo str_repeat("\t", $depth);
  1096. echo "\t\t=> array property \"$name\" set to \"$value\"".PHP_EOL;
  1097. }
  1098. } else {
  1099. $this->ruleset[$code]['properties'][$name] = (string) $prop['value'];
  1100. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1101. echo str_repeat("\t", $depth);
  1102. echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'.PHP_EOL;
  1103. }
  1104. }//end if
  1105. }//end foreach
  1106. }//end if
  1107. // Ignore patterns.
  1108. foreach ($rule->{'exclude-pattern'} as $pattern) {
  1109. if ($this->_shouldProcessElement($pattern) === false) {
  1110. continue;
  1111. }
  1112. if (isset($this->ignorePatterns[$code]) === false) {
  1113. $this->ignorePatterns[$code] = array();
  1114. }
  1115. if (isset($pattern['type']) === false) {
  1116. $pattern['type'] = 'absolute';
  1117. }
  1118. $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
  1119. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1120. echo str_repeat("\t", $depth);
  1121. echo "\t\t=> added sniff-specific ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL;
  1122. }
  1123. }
  1124. }//end _processRule()
  1125. /**
  1126. * Determine if an element should be processed or ignored.
  1127. *
  1128. * @param SimpleXMLElement $element An object from a ruleset XML file.
  1129. * @param int $depth How many nested processing steps we are in.
  1130. * This is only used for debug output.
  1131. *
  1132. * @return bool
  1133. */
  1134. private function _shouldProcessElement($element, $depth=0)
  1135. {
  1136. if (isset($element['phpcbf-only']) === false
  1137. && isset($element['phpcs-only']) === false
  1138. ) {
  1139. // No exceptions are being made.
  1140. return true;
  1141. }
  1142. if (PHP_CODESNIFFER_CBF === true
  1143. && isset($element['phpcbf-only']) === true
  1144. && (string) $element['phpcbf-only'] === 'true'
  1145. ) {
  1146. return true;
  1147. }
  1148. if (PHP_CODESNIFFER_CBF === false
  1149. && isset($element['phpcs-only']) === true
  1150. && (string) $element['phpcs-only'] === 'true'
  1151. ) {
  1152. return true;
  1153. }
  1154. return false;
  1155. }//end _shouldProcessElement()
  1156. /**
  1157. * Loads and stores sniffs objects used for sniffing files.
  1158. *
  1159. * @param array $files Paths to the sniff files to register.
  1160. * @param array $restrictions The sniff class names to restrict the allowed
  1161. * listeners to.
  1162. * @param array $exclusions The sniff class names to exclude from the
  1163. * listeners list.
  1164. *
  1165. * @return void
  1166. * @throws PHP_CodeSniffer_Exception If a sniff file path is invalid.
  1167. */
  1168. public function registerSniffs($files, $restrictions, $exclusions)
  1169. {
  1170. $listeners = array();
  1171. foreach ($files as $file) {
  1172. // Work out where the position of /StandardName/Sniffs/... is
  1173. // so we can determine what the class will be called.
  1174. $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR);
  1175. if ($sniffPos === false) {
  1176. continue;
  1177. }
  1178. $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
  1179. if ($slashPos === false) {
  1180. continue;
  1181. }
  1182. $className = substr($file, ($slashPos + 1));
  1183. if (substr_count($className, DIRECTORY_SEPARATOR) !== 3) {
  1184. throw new PHP_CodeSniffer_Exception("Sniff file $className is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory");
  1185. }
  1186. $className = substr($className, 0, -4);
  1187. $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
  1188. // If they have specified a list of sniffs to restrict to, check
  1189. // to see if this sniff is allowed.
  1190. if (empty($restrictions) === false
  1191. && in_array(strtolower($className), $restrictions) === false
  1192. ) {
  1193. continue;
  1194. }
  1195. // If they have specified a list of sniffs to exclude, check
  1196. // to see if this sniff is allowed.
  1197. if (empty($exclusions) === false
  1198. && in_array(strtolower($className), $exclusions) === true
  1199. ) {
  1200. continue;
  1201. }
  1202. include_once $file;
  1203. // Support the use of PHP namespaces. If the class name we included
  1204. // contains namespace separators instead of underscores, use this as the
  1205. // class name from now on.
  1206. $classNameNS = str_replace('_', '\\', $className);
  1207. if (class_exists($classNameNS, false) === true) {
  1208. $className = $classNameNS;
  1209. }
  1210. // Skip abstract classes.
  1211. $reflection = new ReflectionClass($className);
  1212. if ($reflection->isAbstract() === true) {
  1213. continue;
  1214. }
  1215. $listeners[$className] = $className;
  1216. if (PHP_CODESNIFFER_VERBOSITY > 2) {
  1217. echo "Registered $className".PHP_EOL;
  1218. }
  1219. }//end foreach
  1220. $this->sniffs = $listeners;
  1221. }//end registerSniffs()
  1222. /**
  1223. * Populates the array of PHP_CodeSniffer_Sniff's for this file.
  1224. *
  1225. * @return void
  1226. * @throws PHP_CodeSniffer_Exception If sniff registration fails.
  1227. */
  1228. public function populateTokenListeners()
  1229. {
  1230. // Construct a list of listeners indexed by token being listened for.
  1231. $this->_tokenListeners = array();
  1232. foreach ($this->sniffs as $listenerClass) {
  1233. // Work out the internal code for this sniff. Detect usage of namespace
  1234. // separators instead of underscores to support PHP namespaces.
  1235. if (strstr($listenerClass, '\\') === false) {
  1236. $parts = explode('_', $listenerClass);
  1237. } else {
  1238. $parts = explode('\\', $listenerClass);
  1239. }
  1240. $code = $parts[0].'.'.$parts[2].'.'.$parts[3];
  1241. $code = substr($code, 0, -5);
  1242. $this->listeners[$listenerClass] = new $listenerClass();
  1243. $this->sniffCodes[$code] = $listenerClass;
  1244. // Set custom properties.
  1245. if (isset($this->ruleset[$code]['properties']) === true) {
  1246. foreach ($this->ruleset[$code]['properties'] as $name => $value) {
  1247. $this->setSniffProperty($listenerClass, $name, $value);
  1248. }
  1249. }
  1250. $tokenizers = array();
  1251. $vars = get_class_vars($listenerClass);
  1252. if (isset($vars['supportedTokenizers']) === true) {
  1253. foreach ($vars['supportedTokenizers'] as $tokenizer) {
  1254. $tokenizers[$tokenizer] = $tokenizer;
  1255. }
  1256. } else {
  1257. $tokenizers = array('PHP' => 'PHP');
  1258. }
  1259. $tokens = $this->listeners[$listenerClass]->register();
  1260. if (is_array($tokens) === false) {
  1261. $msg = "Sniff $listenerClass register() method must return an array";
  1262. throw new PHP_CodeSniffer_Exception($msg);
  1263. }
  1264. $parts = explode('_', str_replace('\\', '_', $listenerClass));
  1265. $listenerSource = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5);
  1266. $ignorePatterns = array();
  1267. $patterns = $this->getIgnorePatterns($listenerSource);
  1268. foreach ($patterns as $pattern => $type) {
  1269. // While there is support for a type of each pattern
  1270. // (absolute or relative) we don't actually support it here.
  1271. $replacements = array(
  1272. '\\,' => ',',
  1273. '*' => '.*',
  1274. );
  1275. $ignorePatterns[] = strtr($pattern, $replacements);
  1276. }
  1277. foreach ($tokens as $token) {
  1278. if (isset($this->_tokenListeners[$token]) === false) {
  1279. $this->_tokenListeners[$token] = array();
  1280. }
  1281. if (isset($this->_tokenListeners[$token][$listenerClass]) === false) {
  1282. $this->_tokenListeners[$token][$listenerClass] = array(
  1283. 'class' => $listenerClass,
  1284. 'source' => $listenerSource,
  1285. 'tokenizers' => $tokenizers,
  1286. 'ignore' => $ignorePatterns,
  1287. );
  1288. }
  1289. }
  1290. }//end foreach
  1291. }//end populateTokenListeners()
  1292. /**
  1293. * Set a single property for a sniff.
  1294. *
  1295. * @param string $listenerClass The class name of the sniff.
  1296. * @param string $name The name of the property to change.
  1297. * @param string $value The new value of the property.
  1298. *
  1299. * @return void
  1300. */
  1301. public function setSniffProperty($listenerClass, $name, $value)
  1302. {
  1303. // Setting a property for a sniff we are not using.
  1304. if (isset($this->listeners[$listenerClass]) === false) {
  1305. return;
  1306. }
  1307. $name = trim($name);
  1308. if (is_string($value) === true) {
  1309. $value = trim($value);
  1310. }
  1311. // Special case for booleans.
  1312. if ($value === 'true') {
  1313. $value = true;
  1314. } else if ($value === 'false') {
  1315. $value = false;
  1316. }
  1317. $this->listeners[$listenerClass]->$name = $value;
  1318. }//end setSniffProperty()
  1319. /**
  1320. * Get a list of files that will be processed.
  1321. *
  1322. * If passed directories, this method will find all files within them.
  1323. * The method will also perform file extension and ignore pattern filtering.
  1324. *
  1325. * @param string $paths A list of file or directory paths to process.
  1326. * @param boolean $local If true, only process 1 level of files in directories
  1327. *
  1328. * @return array
  1329. * @throws Exception If there was an error opening a directory.
  1330. * @see shouldProcessFile()
  1331. */
  1332. public function getFilesToProcess($paths, $local=false)
  1333. {
  1334. $files = array();
  1335. foreach ($paths as $path) {
  1336. if (is_dir($path) === true || self::isPharFile($path) === true) {
  1337. if (self::isPharFile($path) === true) {
  1338. $path = 'phar://'.$path;
  1339. }
  1340. if ($local === true) {
  1341. $di = new DirectoryIterator($path);
  1342. } else {
  1343. $di = new RecursiveIteratorIterator(
  1344. new RecursiveDirectoryIterator($path),
  1345. 0,
  1346. RecursiveIteratorIterator::CATCH_GET_CHILD
  1347. );
  1348. }
  1349. foreach ($di as $file) {
  1350. // Check if the file exists after all symlinks are resolved.
  1351. $filePath = self::realpath($file->getPathname());
  1352. if ($filePath === false) {
  1353. continue;
  1354. }
  1355. if (is_dir($filePath) === true) {
  1356. continue;
  1357. }
  1358. if ($this->shouldProcessFile($file->getPathname(), $path) === false) {
  1359. continue;
  1360. }
  1361. $files[] = $file->getPathname();
  1362. }//end foreach
  1363. } else {
  1364. if ($this->shouldIgnoreFile($path, dirname($path)) === true) {
  1365. continue;
  1366. }
  1367. $files[] = $path;
  1368. }//end if
  1369. }//end foreach
  1370. return $files;
  1371. }//end getFilesToProcess()
  1372. /**
  1373. * Checks filtering rules to see if a file should be checked.
  1374. *
  1375. * Checks both file extension filters and path ignore filters.
  1376. *
  1377. * @param string $path The path to the file being checked.
  1378. * @param string $basedir The directory to use for relative path checks.
  1379. *
  1380. * @return bool
  1381. */
  1382. public function shouldProcessFile($path, $basedir)
  1383. {
  1384. // Check that the file's extension is one we are checking.
  1385. // We are strict about checking the extension and we don't
  1386. // let files through with no extension or that start with a dot.
  1387. $fileName = basename($path);
  1388. $fileParts = explode('.', $fileName);
  1389. if ($fileParts[0] === $fileName || $fileParts[0] === '') {
  1390. return false;
  1391. }
  1392. // Checking multi-part file extensions, so need to create a
  1393. // complete extension list and make sure one is allowed.
  1394. $extensions = array();
  1395. array_shift($fileParts);
  1396. foreach ($fileParts as $part) {
  1397. $extensions[implode('.', $fileParts)] = 1;
  1398. array_shift($fileParts);
  1399. }
  1400. $matches = array_intersect_key($extensions, $this->allowedFileExtensions);
  1401. if (empty($matches) === true) {
  1402. return false;
  1403. }
  1404. // If the file's path matches one of our ignore patterns, skip it.
  1405. if ($this->shouldIgnoreFile($path, $basedir) === true) {
  1406. return false;
  1407. }
  1408. return true;
  1409. }//end shouldProcessFile()
  1410. /**
  1411. * Checks filtering rules to see if a file should be ignored.
  1412. *
  1413. * @param string $path The path to the file being checked.
  1414. * @param string $basedir The directory to use for relative path checks.
  1415. *
  1416. * @return bool
  1417. */
  1418. public function shouldIgnoreFile($path, $basedir)
  1419. {
  1420. $relativePath = $path;
  1421. if (strpos($path, $basedir) === 0) {
  1422. // The +1 cuts off the directory separator as well.
  1423. $relativePath = substr($path, (strlen($basedir) + 1));
  1424. }
  1425. foreach ($this->ignorePatterns as $pattern => $type) {
  1426. if (is_array($type) === true) {
  1427. // A sniff specific ignore pattern.
  1428. continue;
  1429. }
  1430. // Maintains backwards compatibility in case the ignore pattern does
  1431. // not have a relative/absolute value.
  1432. if (is_int($pattern) === true) {
  1433. $pattern = $type;
  1434. $type = 'absolute';
  1435. }
  1436. $replacements = array(
  1437. '\\,' => ',',
  1438. '*' => '.*',
  1439. );
  1440. // We assume a / directory separator, as do the exclude rules
  1441. // most developers write, so we need a special case for any system
  1442. // that is different.
  1443. if (DIRECTORY_SEPARATOR === '\\') {
  1444. $replacements['/'] = '\\\\';
  1445. }
  1446. $pattern = strtr($pattern, $replacements);
  1447. if ($type === 'relative') {
  1448. $testPath = $relativePath;
  1449. } else {
  1450. $testPath = $path;
  1451. }
  1452. $pattern = '`'.$pattern.'`i';
  1453. if (preg_match($pattern, $testPath) === 1) {
  1454. return true;
  1455. }
  1456. }//end foreach
  1457. return false;
  1458. }//end shouldIgnoreFile()
  1459. /**
  1460. * Run the code sniffs over a single given file.
  1461. *
  1462. * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it
  1463. * conforms with the standard. Returns the processed file object, or NULL
  1464. * if no file was processed due to error.
  1465. *
  1466. * @param string $file The file to process.
  1467. * @param string $contents The contents to parse. If NULL, the content
  1468. * is taken from the file system.
  1469. *
  1470. * @return PHP_CodeSniffer_File
  1471. * @throws PHP_CodeSniffer_Exception If the file could not be processed.
  1472. * @see _processFile()
  1473. */
  1474. public function processFile($file, $contents=null)
  1475. {
  1476. if ($contents === null && file_exists($file) === false) {
  1477. throw new PHP_CodeSniffer_Exception("Source file $file does not exist");
  1478. }
  1479. $filePath = self::realpath($file);
  1480. if ($filePath === false) {
  1481. $filePath = $file;
  1482. }
  1483. // Before we go and spend time tokenizing this file, just check
  1484. // to see if there is a tag up top to indicate that the whole
  1485. // file should be ignored. It must be on one of the first two lines.
  1486. $firstContent = $contents;
  1487. if ($contents === null && is_readable($filePath) === true) {
  1488. $handle = fopen($filePath, 'r');
  1489. stream_set_blocking($handle, true);
  1490. if ($handle !== false) {
  1491. $firstContent = fgets($handle);
  1492. $firstContent .= fgets($handle);
  1493. fclose($handle);
  1494. if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) {
  1495. // We are ignoring the whole file.
  1496. if (PHP_CODESNIFFER_VERBOSITY > 0) {
  1497. echo 'Ignoring '.basename($filePath).PHP_EOL;
  1498. }
  1499. return null;
  1500. }
  1501. }
  1502. }//end if
  1503. try {
  1504. $phpcsFile = $this->_processFile($file, $contents);
  1505. } catch (Exception $e) {
  1506. $trace = $e->getTrace();
  1507. $filename = $trace[0]['args'][0];
  1508. if (is_object($filename) === true
  1509. && get_class($filename) === 'PHP_CodeSniffer_File'
  1510. ) {
  1511. $filename = $filename->getFilename();
  1512. } else if (is_numeric($filename) === true) {
  1513. // See if we can find the PHP_CodeSniffer_File object.
  1514. foreach ($trace as $data) {
  1515. if (isset($data['args'][0]) === true
  1516. && ($data['args'][0] instanceof PHP_CodeSniffer_File) === true
  1517. ) {
  1518. $filename = $data['args'][0]->getFilename();
  1519. }
  1520. }
  1521. } else if (is_string($filename) === false) {
  1522. $filename = (string) $filename;
  1523. }
  1524. $errorMessage = '"'.$e->getMessage().'" at '.$e->getFile().':'.$e->getLine();
  1525. $error = "An error occurred during processing; checking has been aborted. The error message was: $errorMessage";
  1526. $phpcsFile = new PHP_CodeSniffer_File(
  1527. $filename,
  1528. $this->_tokenListeners,
  1529. $this->ruleset,
  1530. $this
  1531. );
  1532. $phpcsFile->addError($error, null);
  1533. }//end try
  1534. $cliValues = $this->cli->getCommandLineValues();
  1535. if (PHP_CODESNIFFER_INTERACTIVE === false) {
  1536. // Cache the report data for this file so we can unset it to save memory.
  1537. $this->reporting->cacheFileReport($phpcsFile, $cliValues);
  1538. $phpcsFile->cleanUp();
  1539. return $phpcsFile;
  1540. }
  1541. /*
  1542. Running interactively.
  1543. Print the error report for the current file and then wait for user input.
  1544. */
  1545. // Get current violations and then clear the list to make sure
  1546. // we only print violations for a single file each time.
  1547. $numErrors = null;
  1548. while ($numErrors !== 0) {
  1549. $numErrors = ($phpcsFile->getErrorCount() + $phpcsFile->getWarningCount());
  1550. if ($numErrors === 0) {
  1551. continue;
  1552. }
  1553. $reportClass = $this->reporting->factory('full');
  1554. $reportData = $this->reporting->prepareFileReport($phpcsFile);
  1555. $reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']);
  1556. echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
  1557. $input = fgets(STDIN);
  1558. $input = trim($input);
  1559. switch ($input) {
  1560. case 's':
  1561. break(2);
  1562. case 'q':
  1563. exit(0);
  1564. break;
  1565. default:
  1566. // Repopulate the sniffs because some of them save their state
  1567. // and only clear it when the file changes, but we are rechecking
  1568. // the same file.
  1569. $this->populateTokenListeners();
  1570. $phpcsFile = $this->_processFile($file, $contents);
  1571. break;
  1572. }
  1573. }//end while
  1574. return $phpcsFile;
  1575. }//end processFile()
  1576. /**
  1577. * Process the sniffs for a single file.
  1578. *
  1579. * Does raw processing only. No interactive support or error checking.
  1580. *
  1581. * @param string $file The file to process.
  1582. * @param string $contents The contents to parse. If NULL, the content
  1583. * is taken from the file system.
  1584. *
  1585. * @return PHP_CodeSniffer_File
  1586. * @see processFile()
  1587. */
  1588. private function _processFile($file, $contents)
  1589. {
  1590. $stdin = false;
  1591. $cliValues = $this->cli->getCommandLineValues();
  1592. if (empty($cliValues['files']) === true) {
  1593. $stdin = true;
  1594. }
  1595. if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) {
  1596. $startTime = microtime(true);
  1597. echo 'Processing '.basename($file).' ';
  1598. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1599. echo PHP_EOL;
  1600. }
  1601. }
  1602. $phpcsFile = new PHP_CodeSniffer_File(
  1603. $file,
  1604. $this->_tokenListeners,
  1605. $this->ruleset,
  1606. $this
  1607. );
  1608. $phpcsFile->start($contents);
  1609. if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) {
  1610. $timeTaken = ((microtime(true) - $startTime) * 1000);
  1611. if ($timeTaken < 1000) {
  1612. $timeTaken = round($timeTaken);
  1613. echo "DONE in {$timeTaken}ms";
  1614. } else {
  1615. $timeTaken = round(($timeTaken / 1000), 2);
  1616. echo "DONE in $timeTaken secs";
  1617. }
  1618. if (PHP_CODESNIFFER_CBF === true) {
  1619. $errors = $phpcsFile->getFixableCount();
  1620. echo " ($errors fixable violations)".PHP_EOL;
  1621. } else {
  1622. $errors = $phpcsFile->getErrorCount();
  1623. $warnings = $phpcsFile->getWarningCount();
  1624. echo " ($errors errors, $warnings warnings)".PHP_EOL;
  1625. }
  1626. }
  1627. return $phpcsFile;
  1628. }//end _processFile()
  1629. /**
  1630. * Generates documentation for a coding standard.
  1631. *
  1632. * @param string $standard The standard to generate docs for
  1633. * @param array $sniffs A list of sniffs to limit the docs to.
  1634. * @param string $generator The name of the generator class to use.
  1635. *
  1636. * @return void
  1637. */
  1638. public function generateDocs($standard, array $sniffs=array(), $generator='Text')
  1639. {
  1640. if (class_exists('PHP_CodeSniffer_DocGenerators_'.$generator, true) === false) {
  1641. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_'.$generator.' not found');
  1642. }
  1643. $class = "PHP_CodeSniffer_DocGenerators_$generator";
  1644. $generator = new $class($standard, $sniffs);
  1645. $generator->generate();
  1646. }//end generateDocs()
  1647. /**
  1648. * Gets the array of PHP_CodeSniffer_Sniff's.
  1649. *
  1650. * @return PHP_CodeSniffer_Sniff[]
  1651. */
  1652. public function getSniffs()
  1653. {
  1654. return $this->listeners;
  1655. }//end getSniffs()
  1656. /**
  1657. * Gets the array of PHP_CodeSniffer_Sniff's indexed by token type.
  1658. *
  1659. * @return array
  1660. */
  1661. public function getTokenSniffs()
  1662. {
  1663. return $this->_tokenListeners;
  1664. }//end getTokenSniffs()
  1665. /**
  1666. * Returns true if the specified string is in the camel caps format.
  1667. *
  1668. * @param string $string The string the verify.
  1669. * @param boolean $classFormat If true, check to see if the string is in the
  1670. * class format. Class format strings must start
  1671. * with a capital letter and contain no
  1672. * underscores.
  1673. * @param boolean $public If true, the first character in the string
  1674. * must be an a-z character. If false, the
  1675. * character must be an underscore. This
  1676. * argument is only applicable if $classFormat
  1677. * is false.
  1678. * @param boolean $strict If true, the string must not have two capital
  1679. * letters next to each other. If false, a
  1680. * relaxed camel caps policy is used to allow
  1681. * for acronyms.
  1682. *
  1683. * @return boolean
  1684. */
  1685. public static function isCamelCaps(
  1686. $string,
  1687. $classFormat=false,
  1688. $public=true,
  1689. $strict=true
  1690. ) {
  1691. // Check the first character first.
  1692. if ($classFormat === false) {
  1693. $legalFirstChar = '';
  1694. if ($public === false) {
  1695. $legalFirstChar = '[_]';
  1696. }
  1697. if ($strict === false) {
  1698. // Can either start with a lowercase letter, or multiple uppercase
  1699. // in a row, representing an acronym.
  1700. $legalFirstChar .= '([A-Z]{2,}|[a-z])';
  1701. } else {
  1702. $legalFirstChar .= '[a-z]';
  1703. }
  1704. } else {
  1705. $legalFirstChar = '[A-Z]';
  1706. }
  1707. if (preg_match("/^$legalFirstChar/", $string) === 0) {
  1708. return false;
  1709. }
  1710. // Check that the name only contains legal characters.
  1711. $legalChars = 'a-zA-Z0-9';
  1712. if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) {
  1713. return false;
  1714. }
  1715. if ($strict === true) {
  1716. // Check that there are not two capital letters next to each other.
  1717. $length = strlen($string);
  1718. $lastCharWasCaps = $classFormat;
  1719. for ($i = 1; $i < $length; $i++) {
  1720. $ascii = ord($string{$i});
  1721. if ($ascii >= 48 && $ascii <= 57) {
  1722. // The character is a number, so it cant be a capital.
  1723. $isCaps = false;
  1724. } else {
  1725. if (strtoupper($string{$i}) === $string{$i}) {
  1726. $isCaps = true;
  1727. } else {
  1728. $isCaps = false;
  1729. }
  1730. }
  1731. if ($isCaps === true && $lastCharWasCaps === true) {
  1732. return false;
  1733. }
  1734. $lastCharWasCaps = $isCaps;
  1735. }
  1736. }//end if
  1737. return true;
  1738. }//end isCamelCaps()
  1739. /**
  1740. * Returns true if the specified string is in the underscore caps format.
  1741. *
  1742. * @param string $string The string to verify.
  1743. *
  1744. * @return boolean
  1745. */
  1746. public static function isUnderscoreName($string)
  1747. {
  1748. // If there are space in the name, it can't be valid.
  1749. if (strpos($string, ' ') !== false) {
  1750. return false;
  1751. }
  1752. $validName = true;
  1753. $nameBits = explode('_', $string);
  1754. if (preg_match('|^[A-Z]|', $string) === 0) {
  1755. // Name does not begin with a capital letter.
  1756. $validName = false;
  1757. } else {
  1758. foreach ($nameBits as $bit) {
  1759. if ($bit === '') {
  1760. continue;
  1761. }
  1762. if ($bit{0} !== strtoupper($bit{0})) {
  1763. $validName = false;
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. return $validName;
  1769. }//end isUnderscoreName()
  1770. /**
  1771. * Returns a valid variable type for param/var tag.
  1772. *
  1773. * If type is not one of the standard type, it must be a custom type.
  1774. * Returns the correct type name suggestion if type name is invalid.
  1775. *
  1776. * @param string $varType The variable type to process.
  1777. *
  1778. * @return string
  1779. */
  1780. public static function suggestType($varType)
  1781. {
  1782. if ($varType === '') {
  1783. return '';
  1784. }
  1785. if (in_array($varType, self::$allowedTypes) === true) {
  1786. return $varType;
  1787. } else {
  1788. $lowerVarType = strtolower($varType);
  1789. switch ($lowerVarType) {
  1790. case 'bool':
  1791. case 'boolean':
  1792. return 'boolean';
  1793. case 'double':
  1794. case 'real':
  1795. case 'float':
  1796. return 'float';
  1797. case 'int':
  1798. case 'integer':
  1799. return 'integer';
  1800. case 'array()':
  1801. case 'array':
  1802. return 'array';
  1803. }//end switch
  1804. if (strpos($lowerVarType, 'array(') !== false) {
  1805. // Valid array declaration:
  1806. // array, array(type), array(type1 => type2).
  1807. $matches = array();
  1808. $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i';
  1809. if (preg_match($pattern, $varType, $matches) !== 0) {
  1810. $type1 = '';
  1811. if (isset($matches[1]) === true) {
  1812. $type1 = $matches[1];
  1813. }
  1814. $type2 = '';
  1815. if (isset($matches[3]) === true) {
  1816. $type2 = $matches[3];
  1817. }
  1818. $type1 = self::suggestType($type1);
  1819. $type2 = self::suggestType($type2);
  1820. if ($type2 !== '') {
  1821. $type2 = ' => '.$type2;
  1822. }
  1823. return "array($type1$type2)";
  1824. } else {
  1825. return 'array';
  1826. }//end if
  1827. } else if (in_array($lowerVarType, self::$allowedTypes) === true) {
  1828. // A valid type, but not lower cased.
  1829. return $lowerVarType;
  1830. } else {
  1831. // Must be a custom type name.
  1832. return $varType;
  1833. }//end if
  1834. }//end if
  1835. }//end suggestType()
  1836. /**
  1837. * Prepares token content for output to screen.
  1838. *
  1839. * Replaces invisible characters so they are visible. On non-Windows
  1840. * OSes it will also colour the invisible characters.
  1841. *
  1842. * @param string $content The content to prepare.
  1843. *
  1844. * @return string
  1845. */
  1846. public static function prepareForOutput($content)
  1847. {
  1848. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  1849. $content = str_replace("\r", '\r', $content);
  1850. $content = str_replace("\n", '\n', $content);
  1851. $content = str_replace("\t", '\t', $content);
  1852. } else {
  1853. $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
  1854. $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
  1855. $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
  1856. $content = str_replace(' ', "\033[30;1m·\033[0m", $content);
  1857. }
  1858. return $content;
  1859. }//end prepareForOutput()
  1860. /**
  1861. * Get a list paths where standards are installed.
  1862. *
  1863. * @return array
  1864. */
  1865. public static function getInstalledStandardPaths()
  1866. {
  1867. $installedPaths = array(dirname(__FILE__).DIRECTORY_SEPARATOR.'CodeSniffer'.DIRECTORY_SEPARATOR.'Standards');
  1868. $configPaths = PHP_CodeSniffer::getConfigData('installed_paths');
  1869. if ($configPaths !== null) {
  1870. $installedPaths = array_merge($installedPaths, explode(',', $configPaths));
  1871. }
  1872. $resolvedInstalledPaths = array();
  1873. foreach ($installedPaths as $installedPath) {
  1874. if (substr($installedPath, 0, 1) === '.') {
  1875. $installedPath = dirname(__FILE__).DIRECTORY_SEPARATOR.$installedPath;
  1876. }
  1877. $resolvedInstalledPaths[] = $installedPath;
  1878. }
  1879. return $resolvedInstalledPaths;
  1880. }//end getInstalledStandardPaths()
  1881. /**
  1882. * Get a list of all coding standards installed.
  1883. *
  1884. * Coding standards are directories located in the
  1885. * CodeSniffer/Standards directory. Valid coding standards
  1886. * include a Sniffs subdirectory.
  1887. *
  1888. * @param boolean $includeGeneric If true, the special "Generic"
  1889. * coding standard will be included
  1890. * if installed.
  1891. * @param string $standardsDir A specific directory to look for standards
  1892. * in. If not specified, PHP_CodeSniffer will
  1893. * look in its default locations.
  1894. *
  1895. * @return array
  1896. * @see isInstalledStandard()
  1897. */
  1898. public static function getInstalledStandards(
  1899. $includeGeneric=false,
  1900. $standardsDir=''
  1901. ) {
  1902. $installedStandards = array();
  1903. if ($standardsDir === '') {
  1904. $installedPaths = self::getInstalledStandardPaths();
  1905. } else {
  1906. $installedPaths = array($standardsDir);
  1907. }
  1908. foreach ($installedPaths as $standardsDir) {
  1909. $di = new DirectoryIterator($standardsDir);
  1910. foreach ($di as $file) {
  1911. if ($file->isDir() === true && $file->isDot() === false) {
  1912. $filename = $file->getFilename();
  1913. // Ignore the special "Generic" standard.
  1914. if ($includeGeneric === false && $filename === 'Generic') {
  1915. continue;
  1916. }
  1917. // Valid coding standard dirs include a ruleset.
  1918. $csFile = $file->getPathname().'/ruleset.xml';
  1919. if (is_file($csFile) === true) {
  1920. $installedStandards[] = $filename;
  1921. }
  1922. }
  1923. }
  1924. }//end foreach
  1925. return $installedStandards;
  1926. }//end getInstalledStandards()
  1927. /**
  1928. * Determine if a standard is installed.
  1929. *
  1930. * Coding standards are directories located in the
  1931. * CodeSniffer/Standards directory. Valid coding standards
  1932. * include a ruleset.xml file.
  1933. *
  1934. * @param string $standard The name of the coding standard.
  1935. *
  1936. * @return boolean
  1937. * @see getInstalledStandards()
  1938. */
  1939. public static function isInstalledStandard($standard)
  1940. {
  1941. $path = self::getInstalledStandardPath($standard);
  1942. if ($path !== null && strpos($path, 'ruleset.xml') !== false) {
  1943. return true;
  1944. } else {
  1945. // This could be a custom standard, installed outside our
  1946. // standards directory.
  1947. $standard = self::realPath($standard);
  1948. // Might be an actual ruleset file itself.
  1949. // If it has an XML extension, let's at least try it.
  1950. if (is_file($standard) === true
  1951. && (substr(strtolower($standard), -4) === '.xml'
  1952. || substr(strtolower($standard), -9) === '.xml.dist')
  1953. ) {
  1954. return true;
  1955. }
  1956. // If it is a directory with a ruleset.xml file in it,
  1957. // it is a standard.
  1958. $ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml';
  1959. if (is_file($ruleset) === true) {
  1960. return true;
  1961. }
  1962. }//end if
  1963. return false;
  1964. }//end isInstalledStandard()
  1965. /**
  1966. * Return the path of an installed coding standard.
  1967. *
  1968. * Coding standards are directories located in the
  1969. * CodeSniffer/Standards directory. Valid coding standards
  1970. * include a ruleset.xml file.
  1971. *
  1972. * @param string $standard The name of the coding standard.
  1973. *
  1974. * @return string|null
  1975. */
  1976. public static function getInstalledStandardPath($standard)
  1977. {
  1978. $installedPaths = self::getInstalledStandardPaths();
  1979. foreach ($installedPaths as $installedPath) {
  1980. $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard;
  1981. $path = self::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml');
  1982. if (is_file($path) === true) {
  1983. return $path;
  1984. } else if (self::isPharFile($standardPath) === true) {
  1985. $path = self::realpath($standardPath);
  1986. if ($path !== false) {
  1987. return $path;
  1988. }
  1989. }
  1990. }
  1991. return null;
  1992. }//end getInstalledStandardPath()
  1993. /**
  1994. * Get a single config value.
  1995. *
  1996. * Config data is stored in the data dir, in a file called
  1997. * CodeSniffer.conf. It is a simple PHP array.
  1998. *
  1999. * @param string $key The name of the config value.
  2000. *
  2001. * @return string|null
  2002. * @see setConfigData()
  2003. * @see getAllConfigData()
  2004. */
  2005. public static function getConfigData($key)
  2006. {
  2007. $phpCodeSnifferConfig = self::getAllConfigData();
  2008. if ($phpCodeSnifferConfig === null) {
  2009. return null;
  2010. }
  2011. if (isset($phpCodeSnifferConfig[$key]) === false) {
  2012. return null;
  2013. }
  2014. return $phpCodeSnifferConfig[$key];
  2015. }//end getConfigData()
  2016. /**
  2017. * Set a single config value.
  2018. *
  2019. * Config data is stored in the data dir, in a file called
  2020. * CodeSniffer.conf. It is a simple PHP array.
  2021. *
  2022. * @param string $key The name of the config value.
  2023. * @param string|null $value The value to set. If null, the config
  2024. * entry is deleted, reverting it to the
  2025. * default value.
  2026. * @param boolean $temp Set this config data temporarily for this
  2027. * script run. This will not write the config
  2028. * data to the config file.
  2029. *
  2030. * @return boolean
  2031. * @see getConfigData()
  2032. * @throws PHP_CodeSniffer_Exception If the config file can not be written.
  2033. */
  2034. public static function setConfigData($key, $value, $temp=false)
  2035. {
  2036. if ($temp === false) {
  2037. $path = '';
  2038. if (is_callable('Phar::running') === true) {
  2039. $path = Phar::running(false);
  2040. }
  2041. if ($path !== '') {
  2042. $configFile = dirname($path).'/CodeSniffer.conf';
  2043. } else {
  2044. $configFile = dirname(__FILE__).'/CodeSniffer.conf';
  2045. if (is_file($configFile) === false
  2046. && strpos('@data_dir@', '@data_dir') === false
  2047. ) {
  2048. // If data_dir was replaced, this is a PEAR install and we can
  2049. // use the PEAR data dir to store the conf file.
  2050. $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
  2051. }
  2052. }
  2053. if (is_file($configFile) === true
  2054. && is_writable($configFile) === false
  2055. ) {
  2056. $error = 'Config file '.$configFile.' is not writable';
  2057. throw new PHP_CodeSniffer_Exception($error);
  2058. }
  2059. }//end if
  2060. $phpCodeSnifferConfig = self::getAllConfigData();
  2061. if ($value === null) {
  2062. if (isset($phpCodeSnifferConfig[$key]) === true) {
  2063. unset($phpCodeSnifferConfig[$key]);
  2064. }
  2065. } else {
  2066. $phpCodeSnifferConfig[$key] = $value;
  2067. }
  2068. if ($temp === false) {
  2069. $output = '<'.'?php'."\n".' $phpCodeSnifferConfig = ';
  2070. $output .= var_export($phpCodeSnifferConfig, true);
  2071. $output .= "\n?".'>';
  2072. if (file_put_contents($configFile, $output) === false) {
  2073. return false;
  2074. }
  2075. }
  2076. $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig;
  2077. return true;
  2078. }//end setConfigData()
  2079. /**
  2080. * Get all config data in an array.
  2081. *
  2082. * @return array<string, string>
  2083. * @see getConfigData()
  2084. */
  2085. public static function getAllConfigData()
  2086. {
  2087. if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) {
  2088. return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'];
  2089. }
  2090. $path = '';
  2091. if (is_callable('Phar::running') === true) {
  2092. $path = Phar::running(false);
  2093. }
  2094. if ($path !== '') {
  2095. $configFile = dirname($path).'/CodeSniffer.conf';
  2096. } else {
  2097. $configFile = dirname(__FILE__).'/CodeSniffer.conf';
  2098. if (is_file($configFile) === false) {
  2099. $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
  2100. }
  2101. }
  2102. if (is_file($configFile) === false) {
  2103. $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = array();
  2104. return array();
  2105. }
  2106. include $configFile;
  2107. $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig;
  2108. return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'];
  2109. }//end getAllConfigData()
  2110. /**
  2111. * Return TRUE, if the path is a phar file.
  2112. *
  2113. * @param string $path The path to use.
  2114. *
  2115. * @return mixed
  2116. */
  2117. public static function isPharFile($path)
  2118. {
  2119. if (strpos($path, 'phar://') === 0) {
  2120. return true;
  2121. }
  2122. return false;
  2123. }//end isPharFile()
  2124. /**
  2125. * CodeSniffer alternative for realpath.
  2126. *
  2127. * Allows for phar support.
  2128. *
  2129. * @param string $path The path to use.
  2130. *
  2131. * @return mixed
  2132. */
  2133. public static function realpath($path)
  2134. {
  2135. // Support the path replacement of ~ with the user's home directory.
  2136. if (substr($path, 0, 2) === '~/') {
  2137. $homeDir = getenv('HOME');
  2138. if ($homeDir !== false) {
  2139. $path = $homeDir.substr($path, 1);
  2140. }
  2141. }
  2142. // No extra work needed if this is not a phar file.
  2143. if (self::isPharFile($path) === false) {
  2144. return realpath($path);
  2145. }
  2146. // Before trying to break down the file path,
  2147. // check if it exists first because it will mostly not
  2148. // change after running the below code.
  2149. if (file_exists($path) === true) {
  2150. return $path;
  2151. }
  2152. $phar = Phar::running(false);
  2153. $extra = str_replace('phar://'.$phar, '', $path);
  2154. $path = realpath($phar);
  2155. if ($path === false) {
  2156. return false;
  2157. }
  2158. $path = 'phar://'.$path.$extra;
  2159. if (file_exists($path) === true) {
  2160. return $path;
  2161. }
  2162. return false;
  2163. }//end realpath()
  2164. /**
  2165. * CodeSniffer alternative for chdir().
  2166. *
  2167. * Allows for phar support.
  2168. *
  2169. * @param string $path The path to use.
  2170. *
  2171. * @return void
  2172. */
  2173. public static function chdir($path)
  2174. {
  2175. if (self::isPharFile($path) === true) {
  2176. $phar = Phar::running(false);
  2177. chdir(dirname($phar));
  2178. } else {
  2179. chdir($path);
  2180. }
  2181. }//end chdir()
  2182. }//end class