VersionSpecification.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\FixerDefinition;
  13. /**
  14. * @author Andreas Möller <am@localheinz.com>
  15. */
  16. final class VersionSpecification implements VersionSpecificationInterface
  17. {
  18. /**
  19. * @var null|int<1, max>
  20. */
  21. private ?int $minimum;
  22. /**
  23. * @var null|int<1, max>
  24. */
  25. private ?int $maximum;
  26. /**
  27. * @param null|int<1, max> $minimum
  28. * @param null|int<1, max> $maximum
  29. *
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct(?int $minimum = null, ?int $maximum = null)
  33. {
  34. if (null === $minimum && null === $maximum) {
  35. throw new \InvalidArgumentException('Minimum or maximum need to be specified.');
  36. }
  37. if (null !== $minimum && 1 > $minimum) {
  38. throw new \InvalidArgumentException('Minimum needs to be either null or an integer greater than 0.');
  39. }
  40. if (null !== $maximum) {
  41. if (1 > $maximum) {
  42. throw new \InvalidArgumentException('Maximum needs to be either null or an integer greater than 0.');
  43. }
  44. if (null !== $minimum && $maximum < $minimum) {
  45. throw new \InvalidArgumentException('Maximum should not be lower than the minimum.');
  46. }
  47. }
  48. $this->minimum = $minimum;
  49. $this->maximum = $maximum;
  50. }
  51. public function isSatisfiedBy(int $version): bool
  52. {
  53. if (null !== $this->minimum && $version < $this->minimum) {
  54. return false;
  55. }
  56. if (null !== $this->maximum && $version > $this->maximum) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. }