File.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  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\Node;
  11. /**
  12. * Represents a file in the code coverage information tree.
  13. */
  14. final class File extends AbstractNode
  15. {
  16. /**
  17. * @var array
  18. */
  19. private $coverageData;
  20. /**
  21. * @var array
  22. */
  23. private $testData;
  24. /**
  25. * @var int
  26. */
  27. private $numExecutableLines = 0;
  28. /**
  29. * @var int
  30. */
  31. private $numExecutedLines = 0;
  32. /**
  33. * @var array
  34. */
  35. private $classes = [];
  36. /**
  37. * @var array
  38. */
  39. private $traits = [];
  40. /**
  41. * @var array
  42. */
  43. private $functions = [];
  44. /**
  45. * @var array
  46. */
  47. private $linesOfCode = [];
  48. /**
  49. * @var int
  50. */
  51. private $numClasses;
  52. /**
  53. * @var int
  54. */
  55. private $numTestedClasses = 0;
  56. /**
  57. * @var int
  58. */
  59. private $numTraits;
  60. /**
  61. * @var int
  62. */
  63. private $numTestedTraits = 0;
  64. /**
  65. * @var int
  66. */
  67. private $numMethods;
  68. /**
  69. * @var int
  70. */
  71. private $numTestedMethods;
  72. /**
  73. * @var int
  74. */
  75. private $numTestedFunctions;
  76. /**
  77. * @var bool
  78. */
  79. private $cacheTokens;
  80. /**
  81. * @var array
  82. */
  83. private $codeUnitsByLine = [];
  84. public function __construct(string $name, AbstractNode $parent, array $coverageData, array $testData, bool $cacheTokens)
  85. {
  86. parent::__construct($name, $parent);
  87. $this->coverageData = $coverageData;
  88. $this->testData = $testData;
  89. $this->cacheTokens = $cacheTokens;
  90. $this->calculateStatistics();
  91. }
  92. /**
  93. * Returns the number of files in/under this node.
  94. */
  95. public function count(): int
  96. {
  97. return 1;
  98. }
  99. /**
  100. * Returns the code coverage data of this node.
  101. */
  102. public function getCoverageData(): array
  103. {
  104. return $this->coverageData;
  105. }
  106. /**
  107. * Returns the test data of this node.
  108. */
  109. public function getTestData(): array
  110. {
  111. return $this->testData;
  112. }
  113. /**
  114. * Returns the classes of this node.
  115. */
  116. public function getClasses(): array
  117. {
  118. return $this->classes;
  119. }
  120. /**
  121. * Returns the traits of this node.
  122. */
  123. public function getTraits(): array
  124. {
  125. return $this->traits;
  126. }
  127. /**
  128. * Returns the functions of this node.
  129. */
  130. public function getFunctions(): array
  131. {
  132. return $this->functions;
  133. }
  134. /**
  135. * Returns the LOC/CLOC/NCLOC of this node.
  136. */
  137. public function getLinesOfCode(): array
  138. {
  139. return $this->linesOfCode;
  140. }
  141. /**
  142. * Returns the number of executable lines.
  143. */
  144. public function getNumExecutableLines(): int
  145. {
  146. return $this->numExecutableLines;
  147. }
  148. /**
  149. * Returns the number of executed lines.
  150. */
  151. public function getNumExecutedLines(): int
  152. {
  153. return $this->numExecutedLines;
  154. }
  155. /**
  156. * Returns the number of classes.
  157. */
  158. public function getNumClasses(): int
  159. {
  160. if ($this->numClasses === null) {
  161. $this->numClasses = 0;
  162. foreach ($this->classes as $class) {
  163. foreach ($class['methods'] as $method) {
  164. if ($method['executableLines'] > 0) {
  165. $this->numClasses++;
  166. continue 2;
  167. }
  168. }
  169. }
  170. }
  171. return $this->numClasses;
  172. }
  173. /**
  174. * Returns the number of tested classes.
  175. */
  176. public function getNumTestedClasses(): int
  177. {
  178. return $this->numTestedClasses;
  179. }
  180. /**
  181. * Returns the number of traits.
  182. */
  183. public function getNumTraits(): int
  184. {
  185. if ($this->numTraits === null) {
  186. $this->numTraits = 0;
  187. foreach ($this->traits as $trait) {
  188. foreach ($trait['methods'] as $method) {
  189. if ($method['executableLines'] > 0) {
  190. $this->numTraits++;
  191. continue 2;
  192. }
  193. }
  194. }
  195. }
  196. return $this->numTraits;
  197. }
  198. /**
  199. * Returns the number of tested traits.
  200. */
  201. public function getNumTestedTraits(): int
  202. {
  203. return $this->numTestedTraits;
  204. }
  205. /**
  206. * Returns the number of methods.
  207. */
  208. public function getNumMethods(): int
  209. {
  210. if ($this->numMethods === null) {
  211. $this->numMethods = 0;
  212. foreach ($this->classes as $class) {
  213. foreach ($class['methods'] as $method) {
  214. if ($method['executableLines'] > 0) {
  215. $this->numMethods++;
  216. }
  217. }
  218. }
  219. foreach ($this->traits as $trait) {
  220. foreach ($trait['methods'] as $method) {
  221. if ($method['executableLines'] > 0) {
  222. $this->numMethods++;
  223. }
  224. }
  225. }
  226. }
  227. return $this->numMethods;
  228. }
  229. /**
  230. * Returns the number of tested methods.
  231. */
  232. public function getNumTestedMethods(): int
  233. {
  234. if ($this->numTestedMethods === null) {
  235. $this->numTestedMethods = 0;
  236. foreach ($this->classes as $class) {
  237. foreach ($class['methods'] as $method) {
  238. if ($method['executableLines'] > 0 &&
  239. $method['coverage'] === 100) {
  240. $this->numTestedMethods++;
  241. }
  242. }
  243. }
  244. foreach ($this->traits as $trait) {
  245. foreach ($trait['methods'] as $method) {
  246. if ($method['executableLines'] > 0 &&
  247. $method['coverage'] === 100) {
  248. $this->numTestedMethods++;
  249. }
  250. }
  251. }
  252. }
  253. return $this->numTestedMethods;
  254. }
  255. /**
  256. * Returns the number of functions.
  257. */
  258. public function getNumFunctions(): int
  259. {
  260. return \count($this->functions);
  261. }
  262. /**
  263. * Returns the number of tested functions.
  264. */
  265. public function getNumTestedFunctions(): int
  266. {
  267. if ($this->numTestedFunctions === null) {
  268. $this->numTestedFunctions = 0;
  269. foreach ($this->functions as $function) {
  270. if ($function['executableLines'] > 0 &&
  271. $function['coverage'] === 100) {
  272. $this->numTestedFunctions++;
  273. }
  274. }
  275. }
  276. return $this->numTestedFunctions;
  277. }
  278. private function calculateStatistics(): void
  279. {
  280. if ($this->cacheTokens) {
  281. $tokens = \PHP_Token_Stream_CachingFactory::get($this->getPath());
  282. } else {
  283. $tokens = new \PHP_Token_Stream($this->getPath());
  284. }
  285. $this->linesOfCode = $tokens->getLinesOfCode();
  286. foreach (\range(1, $this->linesOfCode['loc']) as $lineNumber) {
  287. $this->codeUnitsByLine[$lineNumber] = [];
  288. }
  289. try {
  290. $this->processClasses($tokens);
  291. $this->processTraits($tokens);
  292. $this->processFunctions($tokens);
  293. } catch (\OutOfBoundsException $e) {
  294. // This can happen with PHP_Token_Stream if the file is syntactically invalid,
  295. // and probably affects a file that wasn't executed.
  296. }
  297. unset($tokens);
  298. foreach (\range(1, $this->linesOfCode['loc']) as $lineNumber) {
  299. if (isset($this->coverageData[$lineNumber])) {
  300. foreach ($this->codeUnitsByLine[$lineNumber] as &$codeUnit) {
  301. $codeUnit['executableLines']++;
  302. }
  303. unset($codeUnit);
  304. $this->numExecutableLines++;
  305. if (\count($this->coverageData[$lineNumber]) > 0) {
  306. foreach ($this->codeUnitsByLine[$lineNumber] as &$codeUnit) {
  307. $codeUnit['executedLines']++;
  308. }
  309. unset($codeUnit);
  310. $this->numExecutedLines++;
  311. }
  312. }
  313. }
  314. foreach ($this->traits as &$trait) {
  315. foreach ($trait['methods'] as &$method) {
  316. if ($method['executableLines'] > 0) {
  317. $method['coverage'] = ($method['executedLines'] /
  318. $method['executableLines']) * 100;
  319. } else {
  320. $method['coverage'] = 100;
  321. }
  322. $method['crap'] = $this->crap(
  323. $method['ccn'],
  324. $method['coverage']
  325. );
  326. $trait['ccn'] += $method['ccn'];
  327. }
  328. unset($method);
  329. if ($trait['executableLines'] > 0) {
  330. $trait['coverage'] = ($trait['executedLines'] /
  331. $trait['executableLines']) * 100;
  332. if ($trait['coverage'] === 100) {
  333. $this->numTestedClasses++;
  334. }
  335. } else {
  336. $trait['coverage'] = 100;
  337. }
  338. $trait['crap'] = $this->crap(
  339. $trait['ccn'],
  340. $trait['coverage']
  341. );
  342. }
  343. unset($trait);
  344. foreach ($this->classes as &$class) {
  345. foreach ($class['methods'] as &$method) {
  346. if ($method['executableLines'] > 0) {
  347. $method['coverage'] = ($method['executedLines'] /
  348. $method['executableLines']) * 100;
  349. } else {
  350. $method['coverage'] = 100;
  351. }
  352. $method['crap'] = $this->crap(
  353. $method['ccn'],
  354. $method['coverage']
  355. );
  356. $class['ccn'] += $method['ccn'];
  357. }
  358. unset($method);
  359. if ($class['executableLines'] > 0) {
  360. $class['coverage'] = ($class['executedLines'] /
  361. $class['executableLines']) * 100;
  362. if ($class['coverage'] === 100) {
  363. $this->numTestedClasses++;
  364. }
  365. } else {
  366. $class['coverage'] = 100;
  367. }
  368. $class['crap'] = $this->crap(
  369. $class['ccn'],
  370. $class['coverage']
  371. );
  372. }
  373. unset($class);
  374. foreach ($this->functions as &$function) {
  375. if ($function['executableLines'] > 0) {
  376. $function['coverage'] = ($function['executedLines'] /
  377. $function['executableLines']) * 100;
  378. } else {
  379. $function['coverage'] = 100;
  380. }
  381. if ($function['coverage'] === 100) {
  382. $this->numTestedFunctions++;
  383. }
  384. $function['crap'] = $this->crap(
  385. $function['ccn'],
  386. $function['coverage']
  387. );
  388. }
  389. }
  390. private function processClasses(\PHP_Token_Stream $tokens): void
  391. {
  392. $classes = $tokens->getClasses();
  393. $link = $this->getId() . '.html#';
  394. foreach ($classes as $className => $class) {
  395. if (\strpos($className, 'anonymous') === 0) {
  396. continue;
  397. }
  398. if (!empty($class['package']['namespace'])) {
  399. $className = $class['package']['namespace'] . '\\' . $className;
  400. }
  401. $this->classes[$className] = [
  402. 'className' => $className,
  403. 'methods' => [],
  404. 'startLine' => $class['startLine'],
  405. 'executableLines' => 0,
  406. 'executedLines' => 0,
  407. 'ccn' => 0,
  408. 'coverage' => 0,
  409. 'crap' => 0,
  410. 'package' => $class['package'],
  411. 'link' => $link . $class['startLine'],
  412. ];
  413. foreach ($class['methods'] as $methodName => $method) {
  414. if (\strpos($methodName, 'anonymous') === 0) {
  415. continue;
  416. }
  417. $this->classes[$className]['methods'][$methodName] = $this->newMethod($methodName, $method, $link);
  418. foreach (\range($method['startLine'], $method['endLine']) as $lineNumber) {
  419. $this->codeUnitsByLine[$lineNumber] = [
  420. &$this->classes[$className],
  421. &$this->classes[$className]['methods'][$methodName],
  422. ];
  423. }
  424. }
  425. }
  426. }
  427. private function processTraits(\PHP_Token_Stream $tokens): void
  428. {
  429. $traits = $tokens->getTraits();
  430. $link = $this->getId() . '.html#';
  431. foreach ($traits as $traitName => $trait) {
  432. $this->traits[$traitName] = [
  433. 'traitName' => $traitName,
  434. 'methods' => [],
  435. 'startLine' => $trait['startLine'],
  436. 'executableLines' => 0,
  437. 'executedLines' => 0,
  438. 'ccn' => 0,
  439. 'coverage' => 0,
  440. 'crap' => 0,
  441. 'package' => $trait['package'],
  442. 'link' => $link . $trait['startLine'],
  443. ];
  444. foreach ($trait['methods'] as $methodName => $method) {
  445. if (\strpos($methodName, 'anonymous') === 0) {
  446. continue;
  447. }
  448. $this->traits[$traitName]['methods'][$methodName] = $this->newMethod($methodName, $method, $link);
  449. foreach (\range($method['startLine'], $method['endLine']) as $lineNumber) {
  450. $this->codeUnitsByLine[$lineNumber] = [
  451. &$this->traits[$traitName],
  452. &$this->traits[$traitName]['methods'][$methodName],
  453. ];
  454. }
  455. }
  456. }
  457. }
  458. private function processFunctions(\PHP_Token_Stream $tokens): void
  459. {
  460. $functions = $tokens->getFunctions();
  461. $link = $this->getId() . '.html#';
  462. foreach ($functions as $functionName => $function) {
  463. if (\strpos($functionName, 'anonymous') === 0) {
  464. continue;
  465. }
  466. $this->functions[$functionName] = [
  467. 'functionName' => $functionName,
  468. 'signature' => $function['signature'],
  469. 'startLine' => $function['startLine'],
  470. 'executableLines' => 0,
  471. 'executedLines' => 0,
  472. 'ccn' => $function['ccn'],
  473. 'coverage' => 0,
  474. 'crap' => 0,
  475. 'link' => $link . $function['startLine'],
  476. ];
  477. foreach (\range($function['startLine'], $function['endLine']) as $lineNumber) {
  478. $this->codeUnitsByLine[$lineNumber] = [&$this->functions[$functionName]];
  479. }
  480. }
  481. }
  482. private function crap(int $ccn, float $coverage): string
  483. {
  484. if ($coverage === 0) {
  485. return (string) ($ccn ** 2 + $ccn);
  486. }
  487. if ($coverage >= 95) {
  488. return (string) $ccn;
  489. }
  490. return \sprintf(
  491. '%01.2F',
  492. $ccn ** 2 * (1 - $coverage / 100) ** 3 + $ccn
  493. );
  494. }
  495. private function newMethod(string $methodName, array $method, string $link): array
  496. {
  497. return [
  498. 'methodName' => $methodName,
  499. 'visibility' => $method['visibility'],
  500. 'signature' => $method['signature'],
  501. 'startLine' => $method['startLine'],
  502. 'endLine' => $method['endLine'],
  503. 'executableLines' => 0,
  504. 'executedLines' => 0,
  505. 'ccn' => $method['ccn'],
  506. 'coverage' => 0,
  507. 'crap' => 0,
  508. 'link' => $link . $method['startLine'],
  509. ];
  510. }
  511. }