OCPSinceChecker.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $this->errors[] = 'PHPDoc is needed for ' . $this->namespace . '\\' . $this->className . '::' . $node->name;
  50. return;
  51. }
  52. $comment = $comments[count($comments) - 1];
  53. $text = $comment->getText();
  54. if(strpos($text, '@deprecated') !== false) {
  55. $this->deprecatedClass = true;
  56. }
  57. if($this->deprecatedClass === false && strpos($text, '@since') === false && strpos($text, '@deprecated') === false) {
  58. $type = $node instanceof \PhpParser\Node\Stmt\Interface_ ? 'interface' : 'class';
  59. $this->errors[] = '@since or @deprecated tag is needed in PHPDoc for ' . $type . ' ' . $this->namespace . '\\' . $this->className;
  60. return;
  61. }
  62. }
  63. if($node instanceof \PhpParser\Node\Stmt\ClassMethod) {
  64. /** @var \PhpParser\Node\Stmt\ClassMethod $node */
  65. /** @var \PhpParser\Comment\Doc[] $comments */
  66. $comments = $node->getAttribute('comments');
  67. if(count($comments) === 0) {
  68. $this->errors[] = 'PHPDoc is needed for ' . $this->namespace . '\\' . $this->className . '::' . $node->name;
  69. return;
  70. }
  71. $comment = $comments[count($comments) - 1];
  72. $text = $comment->getText();
  73. if(strpos($text, '@since') === false && strpos($text, '@deprecated') === false) {
  74. $this->errors[] = '@since or @deprecated tag is needed in PHPDoc for ' . $this->namespace . '\\' . $this->className . '::' . $node->name;
  75. return;
  76. }
  77. }
  78. }
  79. public function getErrors() {
  80. return $this->errors;
  81. }
  82. }
  83. echo 'Parsing all files in lib/public for the presence of @since or @deprecated on each method...' . PHP_EOL . PHP_EOL;
  84. $parser = new PhpParser\Parser(new PhpParser\Lexer);
  85. /* iterate over all .php files in lib/public */
  86. $Directory = new RecursiveDirectoryIterator(dirname(__DIR__) . '/lib/public');
  87. $Iterator = new RecursiveIteratorIterator($Directory);
  88. $Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
  89. $errors = [];
  90. foreach($Regex as $file) {
  91. $stmts = $parser->parse(file_get_contents($file[0]));
  92. $visitor = new SinceTagCheckVisitor($this->blackListedClassNames);
  93. $traverser = new \PhpParser\NodeTraverser();
  94. $traverser->addVisitor($visitor);
  95. $traverser->traverse($stmts);
  96. $errors = array_merge($errors, $visitor->getErrors());
  97. }
  98. if(count($errors)) {
  99. echo join(PHP_EOL, $errors) . PHP_EOL . PHP_EOL;
  100. exit(1);
  101. }