OCPSinceChecker.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. require_once(dirname(__DIR__) . '/3rdparty/autoload.php');
  22. /**
  23. * Class SinceTagCheckVisitor
  24. *
  25. * this class checks all methods for the presence of the @since tag
  26. */
  27. class SinceTagCheckVisitor extends \PhpParser\NodeVisitorAbstract {
  28. /** @var string */
  29. protected $namespace = '';
  30. /** @var string */
  31. protected $className = '';
  32. /** @var bool */
  33. protected $deprecatedClass = false;
  34. /** @var array */
  35. protected $errors = [];
  36. public function enterNode(\PhpParser\Node $node) {
  37. if($this->deprecatedClass) {
  38. return;
  39. }
  40. if($node instanceof \PhpParser\Node\Stmt\Namespace_) {
  41. $this->namespace = $node->name;
  42. }
  43. if($node instanceof \PhpParser\Node\Stmt\Interface_ or
  44. $node instanceof \PhpParser\Node\Stmt\Class_) {
  45. $this->className = $node->name;
  46. /** @var \PhpParser\Comment\Doc[] $comments */
  47. $comments = $node->getAttribute('comments');
  48. if(count($comments) !== 0) {
  49. $comment = $comments[count($comments) - 1];
  50. $text = $comment->getText();
  51. if(strpos($text, '@deprecated') !== false) {
  52. $this->deprecatedClass = true;
  53. }
  54. }
  55. }
  56. if($node instanceof \PhpParser\Node\Stmt\ClassMethod) {
  57. /** @var \PhpParser\Node\Stmt\ClassMethod $node */
  58. /** @var \PhpParser\Comment\Doc[] $comments */
  59. $comments = $node->getAttribute('comments');
  60. if(count($comments) === 0) {
  61. $this->errors[] = 'PHPDoc is needed for ' . $this->namespace . '\\' . $this->className . '::' . $node->name;
  62. return;
  63. }
  64. $comment = $comments[count($comments) - 1];
  65. $text = $comment->getText();
  66. if(strpos($text, '@since') === false && strpos($text, '@deprecated') === false) {
  67. $this->errors[] = '@since or @deprecated tag is needed in PHPDoc for ' . $this->namespace . '\\' . $this->className . '::' . $node->name;
  68. return;
  69. }
  70. }
  71. }
  72. public function getErrors() {
  73. return $this->errors;
  74. }
  75. }
  76. echo 'Parsing all files in lib/public for the presence of @since or @deprecated on each method...' . PHP_EOL . PHP_EOL;
  77. $parser = new PhpParser\Parser(new PhpParser\Lexer);
  78. /* iterate over all .php files in lib/public */
  79. $Directory = new RecursiveDirectoryIterator(dirname(__DIR__) . '/lib/public');
  80. $Iterator = new RecursiveIteratorIterator($Directory);
  81. $Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
  82. $errors = [];
  83. foreach($Regex as $file) {
  84. $stmts = $parser->parse(file_get_contents($file[0]));
  85. $visitor = new SinceTagCheckVisitor($this->blackListedClassNames);
  86. $traverser = new \PhpParser\NodeTraverser();
  87. $traverser->addVisitor($visitor);
  88. $traverser->traverse($stmts);
  89. $errors = array_merge($errors, $visitor->getErrors());
  90. }
  91. if(count($errors)) {
  92. echo join(PHP_EOL, $errors) . PHP_EOL . PHP_EOL;
  93. exit(1);
  94. }