LinesOfCode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/lines-of-code.
  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\LinesOfCode;
  11. /**
  12. * @psalm-immutable
  13. */
  14. final class LinesOfCode
  15. {
  16. /**
  17. * @psalm-var non-negative-int
  18. */
  19. private readonly int $linesOfCode;
  20. /**
  21. * @psalm-var non-negative-int
  22. */
  23. private readonly int $commentLinesOfCode;
  24. /**
  25. * @psalm-var non-negative-int
  26. */
  27. private readonly int $nonCommentLinesOfCode;
  28. /**
  29. * @psalm-var non-negative-int
  30. */
  31. private readonly int $logicalLinesOfCode;
  32. /**
  33. * @psalm-param non-negative-int $linesOfCode
  34. * @psalm-param non-negative-int $commentLinesOfCode
  35. * @psalm-param non-negative-int $nonCommentLinesOfCode
  36. * @psalm-param non-negative-int $logicalLinesOfCode
  37. *
  38. * @throws IllogicalValuesException
  39. * @throws NegativeValueException
  40. */
  41. public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode)
  42. {
  43. /** @psalm-suppress DocblockTypeContradiction */
  44. if ($linesOfCode < 0) {
  45. throw new NegativeValueException('$linesOfCode must not be negative');
  46. }
  47. /** @psalm-suppress DocblockTypeContradiction */
  48. if ($commentLinesOfCode < 0) {
  49. throw new NegativeValueException('$commentLinesOfCode must not be negative');
  50. }
  51. /** @psalm-suppress DocblockTypeContradiction */
  52. if ($nonCommentLinesOfCode < 0) {
  53. throw new NegativeValueException('$nonCommentLinesOfCode must not be negative');
  54. }
  55. /** @psalm-suppress DocblockTypeContradiction */
  56. if ($logicalLinesOfCode < 0) {
  57. throw new NegativeValueException('$logicalLinesOfCode must not be negative');
  58. }
  59. if ($linesOfCode - $commentLinesOfCode !== $nonCommentLinesOfCode) {
  60. throw new IllogicalValuesException('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');
  61. }
  62. $this->linesOfCode = $linesOfCode;
  63. $this->commentLinesOfCode = $commentLinesOfCode;
  64. $this->nonCommentLinesOfCode = $nonCommentLinesOfCode;
  65. $this->logicalLinesOfCode = $logicalLinesOfCode;
  66. }
  67. /**
  68. * @psalm-return non-negative-int
  69. */
  70. public function linesOfCode(): int
  71. {
  72. return $this->linesOfCode;
  73. }
  74. /**
  75. * @psalm-return non-negative-int
  76. */
  77. public function commentLinesOfCode(): int
  78. {
  79. return $this->commentLinesOfCode;
  80. }
  81. /**
  82. * @psalm-return non-negative-int
  83. */
  84. public function nonCommentLinesOfCode(): int
  85. {
  86. return $this->nonCommentLinesOfCode;
  87. }
  88. /**
  89. * @psalm-return non-negative-int
  90. */
  91. public function logicalLinesOfCode(): int
  92. {
  93. return $this->logicalLinesOfCode;
  94. }
  95. public function plus(self $other): self
  96. {
  97. return new self(
  98. $this->linesOfCode() + $other->linesOfCode(),
  99. $this->commentLinesOfCode() + $other->commentLinesOfCode(),
  100. $this->nonCommentLinesOfCode() + $other->nonCommentLinesOfCode(),
  101. $this->logicalLinesOfCode() + $other->logicalLinesOfCode(),
  102. );
  103. }
  104. }