ExecutableLinesFindingVisitor.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of phpunit/php-code-coverage.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
  11. use function array_diff_key;
  12. use function assert;
  13. use function count;
  14. use function current;
  15. use function end;
  16. use function explode;
  17. use function max;
  18. use function preg_match;
  19. use function preg_quote;
  20. use function range;
  21. use function reset;
  22. use function sprintf;
  23. use PhpParser\Node;
  24. use PhpParser\NodeVisitorAbstract;
  25. /**
  26. * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
  27. *
  28. * @psalm-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
  29. */
  30. final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
  31. {
  32. private int $nextBranch = 0;
  33. private readonly string $source;
  34. /**
  35. * @psalm-var LinesType
  36. */
  37. private array $executableLinesGroupedByBranch = [];
  38. /**
  39. * @psalm-var array<int, bool>
  40. */
  41. private array $unsets = [];
  42. /**
  43. * @psalm-var array<int, string>
  44. */
  45. private array $commentsToCheckForUnset = [];
  46. public function __construct(string $source)
  47. {
  48. $this->source = $source;
  49. }
  50. public function enterNode(Node $node): void
  51. {
  52. foreach ($node->getComments() as $comment) {
  53. $commentLine = $comment->getStartLine();
  54. if (!isset($this->executableLinesGroupedByBranch[$commentLine])) {
  55. continue;
  56. }
  57. foreach (explode("\n", $comment->getText()) as $text) {
  58. $this->commentsToCheckForUnset[$commentLine] = $text;
  59. $commentLine++;
  60. }
  61. }
  62. if ($node instanceof Node\Scalar\String_ ||
  63. $node instanceof Node\Scalar\EncapsedStringPart) {
  64. $startLine = $node->getStartLine() + 1;
  65. $endLine = $node->getEndLine() - 1;
  66. if ($startLine <= $endLine) {
  67. foreach (range($startLine, $endLine) as $line) {
  68. unset($this->executableLinesGroupedByBranch[$line]);
  69. }
  70. }
  71. return;
  72. }
  73. if ($node instanceof Node\Stmt\Interface_) {
  74. foreach (range($node->getStartLine(), $node->getEndLine()) as $line) {
  75. $this->unsets[$line] = true;
  76. }
  77. return;
  78. }
  79. if ($node instanceof Node\Stmt\Declare_ ||
  80. $node instanceof Node\Stmt\DeclareDeclare ||
  81. $node instanceof Node\Stmt\Else_ ||
  82. $node instanceof Node\Stmt\EnumCase ||
  83. $node instanceof Node\Stmt\Finally_ ||
  84. $node instanceof Node\Stmt\GroupUse ||
  85. $node instanceof Node\Stmt\Label ||
  86. $node instanceof Node\Stmt\Namespace_ ||
  87. $node instanceof Node\Stmt\Nop ||
  88. $node instanceof Node\Stmt\Switch_ ||
  89. $node instanceof Node\Stmt\TryCatch ||
  90. $node instanceof Node\Stmt\Use_ ||
  91. $node instanceof Node\Stmt\UseUse ||
  92. $node instanceof Node\Expr\ConstFetch ||
  93. $node instanceof Node\Expr\Match_ ||
  94. $node instanceof Node\Expr\Variable ||
  95. $node instanceof Node\Expr\Throw_ ||
  96. $node instanceof Node\ComplexType ||
  97. $node instanceof Node\Const_ ||
  98. $node instanceof Node\Identifier ||
  99. $node instanceof Node\Name ||
  100. $node instanceof Node\Param ||
  101. $node instanceof Node\Scalar) {
  102. return;
  103. }
  104. /*
  105. * nikic/php-parser ^4.18 represents <code>throw</code> statements
  106. * as <code>Stmt\Throw_</code> objects
  107. */
  108. if ($node instanceof Node\Stmt\Throw_) {
  109. $this->setLineBranch($node->expr->getEndLine(), $node->expr->getEndLine(), ++$this->nextBranch);
  110. return;
  111. }
  112. /*
  113. * nikic/php-parser ^5 represents <code>throw</code> statements
  114. * as <code>Stmt\Expression</code> objects that contain an
  115. * <code>Expr\Throw_</code> object
  116. */
  117. if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
  118. $this->setLineBranch($node->expr->expr->getEndLine(), $node->expr->expr->getEndLine(), ++$this->nextBranch);
  119. return;
  120. }
  121. if ($node instanceof Node\Stmt\Enum_ ||
  122. $node instanceof Node\Stmt\Function_ ||
  123. $node instanceof Node\Stmt\Class_ ||
  124. $node instanceof Node\Stmt\ClassMethod ||
  125. $node instanceof Node\Expr\Closure ||
  126. $node instanceof Node\Stmt\Trait_) {
  127. if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
  128. $unsets = [];
  129. foreach ($node->getParams() as $param) {
  130. foreach (range($param->getStartLine(), $param->getEndLine()) as $line) {
  131. $unsets[$line] = true;
  132. }
  133. }
  134. unset($unsets[$node->getEndLine()]);
  135. $this->unsets += $unsets;
  136. }
  137. $isConcreteClassLike = $node instanceof Node\Stmt\Enum_ || $node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_;
  138. if (null !== $node->stmts) {
  139. foreach ($node->stmts as $stmt) {
  140. if ($stmt instanceof Node\Stmt\Nop) {
  141. continue;
  142. }
  143. foreach (range($stmt->getStartLine(), $stmt->getEndLine()) as $line) {
  144. unset($this->executableLinesGroupedByBranch[$line]);
  145. if (
  146. $isConcreteClassLike &&
  147. !$stmt instanceof Node\Stmt\ClassMethod
  148. ) {
  149. $this->unsets[$line] = true;
  150. }
  151. }
  152. }
  153. }
  154. if ($isConcreteClassLike) {
  155. return;
  156. }
  157. $hasEmptyBody = [] === $node->stmts ||
  158. null === $node->stmts ||
  159. (
  160. 1 === count($node->stmts) &&
  161. $node->stmts[0] instanceof Node\Stmt\Nop
  162. );
  163. if ($hasEmptyBody) {
  164. if ($node->getEndLine() === $node->getStartLine() && isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
  165. return;
  166. }
  167. $this->setLineBranch($node->getEndLine(), $node->getEndLine(), ++$this->nextBranch);
  168. return;
  169. }
  170. return;
  171. }
  172. if ($node instanceof Node\Expr\ArrowFunction) {
  173. $startLine = max(
  174. $node->getStartLine() + 1,
  175. $node->expr->getStartLine(),
  176. );
  177. $endLine = $node->expr->getEndLine();
  178. if ($endLine < $startLine) {
  179. return;
  180. }
  181. $this->setLineBranch($startLine, $endLine, ++$this->nextBranch);
  182. return;
  183. }
  184. if ($node instanceof Node\Expr\Ternary) {
  185. if (null !== $node->if &&
  186. $node->getStartLine() !== $node->if->getEndLine()) {
  187. $this->setLineBranch($node->if->getStartLine(), $node->if->getEndLine(), ++$this->nextBranch);
  188. }
  189. if ($node->getStartLine() !== $node->else->getEndLine()) {
  190. $this->setLineBranch($node->else->getStartLine(), $node->else->getEndLine(), ++$this->nextBranch);
  191. }
  192. return;
  193. }
  194. if ($node instanceof Node\Expr\BinaryOp\Coalesce) {
  195. if ($node->getStartLine() !== $node->getEndLine()) {
  196. $this->setLineBranch($node->getEndLine(), $node->getEndLine(), ++$this->nextBranch);
  197. }
  198. return;
  199. }
  200. if ($node instanceof Node\Stmt\If_ ||
  201. $node instanceof Node\Stmt\ElseIf_ ||
  202. $node instanceof Node\Stmt\Case_) {
  203. if (null === $node->cond) {
  204. return;
  205. }
  206. $this->setLineBranch(
  207. $node->cond->getStartLine(),
  208. $node->cond->getStartLine(),
  209. ++$this->nextBranch,
  210. );
  211. return;
  212. }
  213. if ($node instanceof Node\Stmt\For_) {
  214. $startLine = null;
  215. $endLine = null;
  216. if ([] !== $node->init) {
  217. $startLine = $node->init[0]->getStartLine();
  218. end($node->init);
  219. $endLine = current($node->init)->getEndLine();
  220. reset($node->init);
  221. }
  222. if ([] !== $node->cond) {
  223. if (null === $startLine) {
  224. $startLine = $node->cond[0]->getStartLine();
  225. }
  226. end($node->cond);
  227. $endLine = current($node->cond)->getEndLine();
  228. reset($node->cond);
  229. }
  230. if ([] !== $node->loop) {
  231. if (null === $startLine) {
  232. $startLine = $node->loop[0]->getStartLine();
  233. }
  234. end($node->loop);
  235. $endLine = current($node->loop)->getEndLine();
  236. reset($node->loop);
  237. }
  238. if (null === $startLine || null === $endLine) {
  239. return;
  240. }
  241. $this->setLineBranch(
  242. $startLine,
  243. $endLine,
  244. ++$this->nextBranch,
  245. );
  246. return;
  247. }
  248. if ($node instanceof Node\Stmt\Foreach_) {
  249. $this->setLineBranch(
  250. $node->expr->getStartLine(),
  251. $node->valueVar->getEndLine(),
  252. ++$this->nextBranch,
  253. );
  254. return;
  255. }
  256. if ($node instanceof Node\Stmt\While_ ||
  257. $node instanceof Node\Stmt\Do_) {
  258. $this->setLineBranch(
  259. $node->cond->getStartLine(),
  260. $node->cond->getEndLine(),
  261. ++$this->nextBranch,
  262. );
  263. return;
  264. }
  265. if ($node instanceof Node\Stmt\Catch_) {
  266. assert([] !== $node->types);
  267. $startLine = $node->types[0]->getStartLine();
  268. end($node->types);
  269. $endLine = current($node->types)->getEndLine();
  270. $this->setLineBranch(
  271. $startLine,
  272. $endLine,
  273. ++$this->nextBranch,
  274. );
  275. return;
  276. }
  277. if ($node instanceof Node\Expr\CallLike) {
  278. if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
  279. $branch = $this->executableLinesGroupedByBranch[$node->getStartLine()];
  280. } else {
  281. $branch = ++$this->nextBranch;
  282. }
  283. $this->setLineBranch($node->getStartLine(), $node->getEndLine(), $branch);
  284. return;
  285. }
  286. if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
  287. return;
  288. }
  289. $this->setLineBranch($node->getStartLine(), $node->getEndLine(), ++$this->nextBranch);
  290. }
  291. public function afterTraverse(array $nodes): void
  292. {
  293. $lines = explode("\n", $this->source);
  294. foreach ($lines as $lineNumber => $line) {
  295. $lineNumber++;
  296. if (1 === preg_match('/^\s*$/', $line) ||
  297. (
  298. isset($this->commentsToCheckForUnset[$lineNumber]) &&
  299. 1 === preg_match(sprintf('/^\s*%s\s*$/', preg_quote($this->commentsToCheckForUnset[$lineNumber], '/')), $line)
  300. )) {
  301. unset($this->executableLinesGroupedByBranch[$lineNumber]);
  302. }
  303. }
  304. $this->executableLinesGroupedByBranch = array_diff_key(
  305. $this->executableLinesGroupedByBranch,
  306. $this->unsets,
  307. );
  308. }
  309. /**
  310. * @psalm-return LinesType
  311. */
  312. public function executableLinesGroupedByBranch(): array
  313. {
  314. return $this->executableLinesGroupedByBranch;
  315. }
  316. private function setLineBranch(int $start, int $end, int $branch): void
  317. {
  318. foreach (range($start, $end) as $line) {
  319. $this->executableLinesGroupedByBranch[$line] = $branch;
  320. }
  321. }
  322. }