Code Duplication    Length = 9-10 lines in 20 locations

src/Assert.php 20 locations

@@ 81-89 (lines=9) @@
78
     *
79
     * @throws InvalidArgumentException
80
     */
81
    public static function integer($value, $message = '')
82
    {
83
        if (!\is_int($value)) {
84
            static::reportInvalidArgument(\sprintf(
85
                $message ?: 'Expected an integer. Got: %s',
86
                static::typeToString($value)
87
            ));
88
        }
89
    }
90
91
    /**
92
     * @psalm-pure
@@ 119-127 (lines=9) @@
116
     *
117
     * @throws InvalidArgumentException
118
     */
119
    public static function positiveInteger($value, $message = '')
120
    {
121
        if (! (\is_int($value) && $value > 0)) {
122
            static::reportInvalidArgument(\sprintf(
123
                $message ?: 'Expected a positive integer. Got: %s',
124
                static::valueToString($value)
125
            ));
126
        }
127
    }
128
129
    /**
130
     * @psalm-pure
@@ 138-146 (lines=9) @@
135
     *
136
     * @throws InvalidArgumentException
137
     */
138
    public static function naturalNumber($value, $message = '')
139
    {
140
        if (! (\is_int($value) && $value > 0)) {
141
            static::reportInvalidArgument(\sprintf(
142
                $message ?: 'Expected a positive integer. Got: %s',
143
                static::valueToString($value)
144
            ));
145
        }
146
    }
147
148
    /**
149
     * @psalm-pure
@@ 195-203 (lines=9) @@
192
     *
193
     * @throws InvalidArgumentException
194
     */
195
    public static function natural($value, $message = '')
196
    {
197
        if (!\is_int($value) || $value < 0) {
198
            static::reportInvalidArgument(\sprintf(
199
                $message ?: 'Expected a non-negative integer. Got: %s',
200
                static::valueToString($value)
201
            ));
202
        }
203
    }
204
205
    /**
206
     * @psalm-pure
@@ 615-623 (lines=9) @@
612
     *
613
     * @throws InvalidArgumentException
614
     */
615
    public static function null($value, $message = '')
616
    {
617
        if (null !== $value) {
618
            static::reportInvalidArgument(\sprintf(
619
                $message ?: 'Expected null. Got: %s',
620
                static::valueToString($value)
621
            ));
622
        }
623
    }
624
625
    /**
626
     * @psalm-pure
@@ 652-660 (lines=9) @@
649
     *
650
     * @throws InvalidArgumentException
651
     */
652
    public static function true($value, $message = '')
653
    {
654
        if (true !== $value) {
655
            static::reportInvalidArgument(\sprintf(
656
                $message ?: 'Expected a value to be true. Got: %s',
657
                static::valueToString($value)
658
            ));
659
        }
660
    }
661
662
    /**
663
     * @psalm-pure
@@ 671-679 (lines=9) @@
668
     *
669
     * @throws InvalidArgumentException
670
     */
671
    public static function false($value, $message = '')
672
    {
673
        if (false !== $value) {
674
            static::reportInvalidArgument(\sprintf(
675
                $message ?: 'Expected a value to be false. Got: %s',
676
                static::valueToString($value)
677
            ));
678
        }
679
    }
680
681
    /**
682
     * @psalm-pure
@@ 794-803 (lines=10) @@
791
     *
792
     * @throws InvalidArgumentException
793
     */
794
    public static function eq($value, $expect, $message = '')
795
    {
796
        if ($expect != $value) {
797
            static::reportInvalidArgument(\sprintf(
798
                $message ?: 'Expected a value equal to %2$s. Got: %s',
799
                static::valueToString($value),
800
                static::valueToString($expect)
801
            ));
802
        }
803
    }
804
805
    /**
806
     * @param mixed  $value
@@ 831-840 (lines=10) @@
828
     *
829
     * @throws InvalidArgumentException
830
     */
831
    public static function same($value, $expect, $message = '')
832
    {
833
        if ($expect !== $value) {
834
            static::reportInvalidArgument(\sprintf(
835
                $message ?: 'Expected a value identical to %2$s. Got: %s',
836
                static::valueToString($value),
837
                static::valueToString($expect)
838
            ));
839
        }
840
    }
841
842
    /**
843
     * @psalm-pure
@@ 870-879 (lines=10) @@
867
     *
868
     * @throws InvalidArgumentException
869
     */
870
    public static function greaterThan($value, $limit, $message = '')
871
    {
872
        if ($value <= $limit) {
873
            static::reportInvalidArgument(\sprintf(
874
                $message ?: 'Expected a value greater than %2$s. Got: %s',
875
                static::valueToString($value),
876
                static::valueToString($limit)
877
            ));
878
        }
879
    }
880
881
    /**
882
     * @psalm-pure
@@ 890-899 (lines=10) @@
887
     *
888
     * @throws InvalidArgumentException
889
     */
890
    public static function greaterThanEq($value, $limit, $message = '')
891
    {
892
        if ($value < $limit) {
893
            static::reportInvalidArgument(\sprintf(
894
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
895
                static::valueToString($value),
896
                static::valueToString($limit)
897
            ));
898
        }
899
    }
900
901
    /**
902
     * @psalm-pure
@@ 910-919 (lines=10) @@
907
     *
908
     * @throws InvalidArgumentException
909
     */
910
    public static function lessThan($value, $limit, $message = '')
911
    {
912
        if ($value >= $limit) {
913
            static::reportInvalidArgument(\sprintf(
914
                $message ?: 'Expected a value less than %2$s. Got: %s',
915
                static::valueToString($value),
916
                static::valueToString($limit)
917
            ));
918
        }
919
    }
920
921
    /**
922
     * @psalm-pure
@@ 930-939 (lines=10) @@
927
     *
928
     * @throws InvalidArgumentException
929
     */
930
    public static function lessThanEq($value, $limit, $message = '')
931
    {
932
        if ($value > $limit) {
933
            static::reportInvalidArgument(\sprintf(
934
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
935
                static::valueToString($value),
936
                static::valueToString($limit)
937
            ));
938
        }
939
    }
940
941
    /**
942
     * Inclusive range, so Assert::(3, 3, 5) passes.
@@ 1051-1059 (lines=9) @@
1048
     *
1049
     * @throws InvalidArgumentException
1050
     */
1051
    public static function notWhitespaceOnly($value, $message = '')
1052
    {
1053
        if (\preg_match('/^\s*$/', $value)) {
1054
            static::reportInvalidArgument(\sprintf(
1055
                $message ?: 'Expected a non-whitespace string. Got: %s',
1056
                static::valueToString($value)
1057
            ));
1058
        }
1059
    }
1060
1061
    /**
1062
     * @psalm-pure
@@ 1380-1389 (lines=10) @@
1377
     *
1378
     * @throws InvalidArgumentException
1379
     */
1380
    public static function minLength($value, $min, $message = '')
1381
    {
1382
        if (static::strlen($value) < $min) {
1383
            static::reportInvalidArgument(\sprintf(
1384
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
1385
                static::valueToString($value),
1386
                $min
1387
            ));
1388
        }
1389
    }
1390
1391
    /**
1392
     * Inclusive max.
@@ 1402-1411 (lines=10) @@
1399
     *
1400
     * @throws InvalidArgumentException
1401
     */
1402
    public static function maxLength($value, $max, $message = '')
1403
    {
1404
        if (static::strlen($value) > $max) {
1405
            static::reportInvalidArgument(\sprintf(
1406
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
1407
                static::valueToString($value),
1408
                $max
1409
            ));
1410
        }
1411
    }
1412
1413
    /**
1414
     * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
@@ 1501-1509 (lines=9) @@
1498
     *
1499
     * @throws InvalidArgumentException
1500
     */
1501
    public static function readable($value, $message = '')
1502
    {
1503
        if (!\is_readable($value)) {
1504
            static::reportInvalidArgument(\sprintf(
1505
                $message ?: 'The path %s is not readable.',
1506
                static::valueToString($value)
1507
            ));
1508
        }
1509
    }
1510
1511
    /**
1512
     * @param string $value
@@ 1517-1525 (lines=9) @@
1514
     *
1515
     * @throws InvalidArgumentException
1516
     */
1517
    public static function writable($value, $message = '')
1518
    {
1519
        if (!\is_writable($value)) {
1520
            static::reportInvalidArgument(\sprintf(
1521
                $message ?: 'The path %s is not writable.',
1522
                static::valueToString($value)
1523
            ));
1524
        }
1525
    }
1526
1527
    /**
1528
     * @psalm-assert class-string $value
@@ 1535-1543 (lines=9) @@
1532
     *
1533
     * @throws InvalidArgumentException
1534
     */
1535
    public static function classExists($value, $message = '')
1536
    {
1537
        if (!\class_exists($value)) {
1538
            static::reportInvalidArgument(\sprintf(
1539
                $message ?: 'Expected an existing class name. Got: %s',
1540
                static::valueToString($value)
1541
            ));
1542
        }
1543
    }
1544
1545
    /**
1546
     * @psalm-pure
@@ 1576-1584 (lines=9) @@
1573
     *
1574
     * @throws InvalidArgumentException
1575
     */
1576
    public static function interfaceExists($value, $message = '')
1577
    {
1578
        if (!\interface_exists($value)) {
1579
            static::reportInvalidArgument(\sprintf(
1580
                $message ?: 'Expected an existing interface name. got %s',
1581
                static::valueToString($value)
1582
            ));
1583
        }
1584
    }
1585
1586
    /**
1587
     * @psalm-pure