LinesOfCode.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @var int
  18. */
  19. private $linesOfCode;
  20. /**
  21. * @var int
  22. */
  23. private $commentLinesOfCode;
  24. /**
  25. * @var int
  26. */
  27. private $nonCommentLinesOfCode;
  28. /**
  29. * @var int
  30. */
  31. private $logicalLinesOfCode;
  32. /**
  33. * @throws IllogicalValuesException
  34. * @throws NegativeValueException
  35. */
  36. public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode)
  37. {
  38. if ($linesOfCode < 0) {
  39. throw new NegativeValueException('$linesOfCode must not be negative');
  40. }
  41. if ($commentLinesOfCode < 0) {
  42. throw new NegativeValueException('$commentLinesOfCode must not be negative');
  43. }
  44. if ($nonCommentLinesOfCode < 0) {
  45. throw new NegativeValueException('$nonCommentLinesOfCode must not be negative');
  46. }
  47. if ($logicalLinesOfCode < 0) {
  48. throw new NegativeValueException('$logicalLinesOfCode must not be negative');
  49. }
  50. if ($linesOfCode - $commentLinesOfCode !== $nonCommentLinesOfCode) {
  51. throw new IllogicalValuesException('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');
  52. }
  53. $this->linesOfCode = $linesOfCode;
  54. $this->commentLinesOfCode = $commentLinesOfCode;
  55. $this->nonCommentLinesOfCode = $nonCommentLinesOfCode;
  56. $this->logicalLinesOfCode = $logicalLinesOfCode;
  57. }
  58. public function linesOfCode(): int
  59. {
  60. return $this->linesOfCode;
  61. }
  62. public function commentLinesOfCode(): int
  63. {
  64. return $this->commentLinesOfCode;
  65. }
  66. public function nonCommentLinesOfCode(): int
  67. {
  68. return $this->nonCommentLinesOfCode;
  69. }
  70. public function logicalLinesOfCode(): int
  71. {
  72. return $this->logicalLinesOfCode;
  73. }
  74. public function plus(self $other): self
  75. {
  76. return new self(
  77. $this->linesOfCode() + $other->linesOfCode(),
  78. $this->commentLinesOfCode() + $other->commentLinesOfCode(),
  79. $this->nonCommentLinesOfCode() + $other->nonCommentLinesOfCode(),
  80. $this->logicalLinesOfCode() + $other->logicalLinesOfCode(),
  81. );
  82. }
  83. }