PageRenderTime 623ms CodeModel.GetById 19ms RepoModel.GetById 8ms app.codeStats 0ms

/application/tests/CodeSniffer/Standards/Ontowiki/Sniffs/Classes/ClassFilePathSniff.php

https://code.google.com/p/ontowiki/
PHP | 112 lines | 44 code | 12 blank | 56 comment | 6 complexity | 4a1cfd9b46f4b96a3de1b86ca4b2f867 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Ontowiki_Sniffs_Classes_ClassFilePathSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Lars Eidam <lars.eidam@googlemail.com>
  10. * @copyright
  11. * @license
  12. * @version
  13. * @link http://code.google.com/p/ontowiki/
  14. */
  15. /**
  16. * Ontowiki_Sniffs_Classes_ClassFilePathSniff.
  17. *
  18. * Tests that the filepath correspond to the classname for php Files in
  19. * the application/classes Folder
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Lars Eidam <lars.eidam@googlemail.com>
  24. * @copyright
  25. * @license
  26. * @version
  27. * @link http://code.google.com/p/ontowiki/
  28. */
  29. class Ontowiki_Sniffs_Classes_ClassFilePathSniff implements PHP_CodeSniffer_Sniff
  30. {
  31. /**
  32. * Returns an array of tokens this test wants to listen for.
  33. *
  34. * @return array
  35. */
  36. public function register()
  37. {
  38. return array(
  39. T_CLASS,
  40. );
  41. }//end register()
  42. /**
  43. * Processes this test, when one of its tokens is encountered.
  44. *
  45. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token in the
  47. * stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  52. {
  53. $tokens = $phpcsFile->getTokens();
  54. $decName = $phpcsFile->findNext(T_STRING, $stackPtr);
  55. $fullPath = $phpcsFile->getFilename();
  56. $longFileName = basename($fullPath);
  57. $fileName = substr($longFileName, 0, strrpos($longFileName, '.'));
  58. // if the file is under the application/classes folder the class has the path in the name
  59. // application/classes/Ontowiki/Utils/TestClass.php -> Classname=Ontowiki_Utils_TestClass
  60. if (stristr($fullPath, 'classes') !== FALSE) {
  61. $partedPath = substr($fullPath, strrpos($fullPath, 'classes'), strlen($fullPath));
  62. $partedPath = substr($partedPath, 0, strrpos($partedPath, '.'));
  63. $classNameArray = explode("_", $tokens[$decName]['content']);
  64. $filepathArray = explode("/", $partedPath);
  65. if (1 == count($filepathArray)) {
  66. $filepathArray = explode("\\", $partedPath);
  67. }
  68. $notFound = TRUE;
  69. foreach ($classNameArray as $index => $classNamePart) {
  70. if ($classNamePart != $filepathArray[$index + 1]) {
  71. $notFound = FALSE;
  72. break;
  73. }
  74. }
  75. if (FALSE === $notFound) {
  76. $error = '%s name doesn\'t match filepath; expected "%s %s"';
  77. $data = array(
  78. ucfirst($tokens[$stackPtr]['content']),
  79. $tokens[$stackPtr]['content'],
  80. $tokens[$decName]['content'],
  81. );
  82. $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
  83. }
  84. }
  85. // else
  86. // {
  87. // if ($tokens[$decName]['content'] !== $fileName)
  88. // {
  89. // $error = '%s name doesn\'t match filename; expected "%s %s"';
  90. // $data = array(
  91. // ucfirst($tokens[$stackPtr]['content']),
  92. // $tokens[$stackPtr]['content'],
  93. // $fileName,
  94. // );
  95. // $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
  96. // }
  97. // }
  98. }//end process()
  99. }//end class
  100. ?>