1
0

codechecker.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\App;
  23. use OC\Hooks\BasicEmitter;
  24. use PhpParser\Lexer;
  25. use PhpParser\Node;
  26. use PhpParser\Node\Name;
  27. use PhpParser\NodeTraverser;
  28. use PhpParser\NodeVisitorAbstract;
  29. use PhpParser\Parser;
  30. use RecursiveCallbackFilterIterator;
  31. use RecursiveDirectoryIterator;
  32. use RecursiveIteratorIterator;
  33. use RegexIterator;
  34. use SplFileInfo;
  35. class CodeChecker extends BasicEmitter {
  36. const CLASS_EXTENDS_NOT_ALLOWED = 1000;
  37. const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001;
  38. const STATIC_CALL_NOT_ALLOWED = 1002;
  39. const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
  40. const CLASS_NEW_FETCH_NOT_ALLOWED = 1004;
  41. /** @var Parser */
  42. private $parser;
  43. /** @var string[] */
  44. private $blackListedClassNames;
  45. public function __construct() {
  46. $this->parser = new Parser(new Lexer);
  47. $this->blackListedClassNames = [
  48. // classes replaced by the public api
  49. 'OC_API',
  50. 'OC_App',
  51. 'OC_AppConfig',
  52. 'OC_Avatar',
  53. 'OC_BackgroundJob',
  54. 'OC_Config',
  55. 'OC_DB',
  56. 'OC_Files',
  57. 'OC_Helper',
  58. 'OC_Hook',
  59. 'OC_Image',
  60. 'OC_JSON',
  61. 'OC_L10N',
  62. 'OC_Log',
  63. 'OC_Mail',
  64. 'OC_Preferences',
  65. 'OC_Request',
  66. 'OC_Response',
  67. 'OC_Template',
  68. 'OC_User',
  69. 'OC_Util',
  70. ];
  71. }
  72. /**
  73. * @param string $appId
  74. * @return array
  75. */
  76. public function analyse($appId) {
  77. $appPath = \OC_App::getAppPath($appId);
  78. if ($appPath === false) {
  79. throw new \RuntimeException("No app with given id <$appId> known.");
  80. }
  81. return $this->analyseFolder($appPath);
  82. }
  83. /**
  84. * @param string $folder
  85. * @return array
  86. */
  87. public function analyseFolder($folder) {
  88. $errors = [];
  89. $excludes = array_map(function($item) use ($folder) {
  90. return $folder . '/' . $item;
  91. }, ['vendor', '3rdparty', '.git', 'l10n']);
  92. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  93. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  94. /** @var SplFileInfo $item */
  95. foreach($excludes as $exclude) {
  96. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. });
  102. $iterator = new RecursiveIteratorIterator($iterator);
  103. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  104. foreach ($iterator as $file) {
  105. /** @var SplFileInfo $file */
  106. $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]);
  107. $fileErrors = $this->analyseFile($file);
  108. $this->emit('CodeChecker', 'analyseFileFinished', [$fileErrors]);
  109. $errors = array_merge($fileErrors, $errors);
  110. }
  111. return $errors;
  112. }
  113. /**
  114. * @param string $file
  115. * @return array
  116. */
  117. public function analyseFile($file) {
  118. $code = file_get_contents($file);
  119. $statements = $this->parser->parse($code);
  120. $visitor = new CodeCheckVisitor($this->blackListedClassNames);
  121. $traverser = new NodeTraverser;
  122. $traverser->addVisitor($visitor);
  123. $traverser->traverse($statements);
  124. return $visitor->errors;
  125. }
  126. }