Issues (26)

src/Check.php (3 issues)

1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit;
5
6
use Pitchart\Phlunit\Checks\ArrayCheck;
7
use Pitchart\Phlunit\Checks\BooleanCheck;
8
use Pitchart\Phlunit\Checks\CallableCheck;
9
use Pitchart\Phlunit\Checks\CollectionCheck;
10
use Pitchart\Phlunit\Checks\DateTimeCheck;
11
use Pitchart\Phlunit\Checks\ExceptionCheck;
12
use Pitchart\Phlunit\Checks\FloatCheck;
13
use Pitchart\Phlunit\Checks\FluentCheck;
14
use Pitchart\Phlunit\Checks\GenericCheck;
15
use Pitchart\Phlunit\Checks\IntegerCheck;
16
use Pitchart\Phlunit\Checks\ResponseCheck;
17
use Pitchart\Phlunit\Checks\StringCheck;
18
use Psr\Http\Message\ResponseInterface;
19
20
final class Check
21
{
22
    /**
23
     * @var array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
24
     */
25
    private static $assertionClassesMap = [
26
        'string' => StringCheck::class,
27
        'boolean' => BooleanCheck::class,
28
        'integer' => IntegerCheck::class,
29
        'float' => FloatCheck::class,
30
        'double' => FloatCheck::class,
31
        'array' => ArrayCheck::class,
32
        'iterable' => CollectionCheck::class,
33
        'callable' => CallableCheck::class,
34
        \Throwable::class => ExceptionCheck::class,
35
        ResponseInterface::class => ResponseCheck::class,
36
        \DateTimeInterface::class => DateTimeCheck::class,
37
    ];
38
39 341
    private static function hasAssertionClass(string $type): bool
40
    {
41 341
        return isset(self::$assertionClassesMap[$type]) && \class_exists(self::$assertionClassesMap[$type]);
42
    }
43
44
45
    /**
46
     * @template T
47
     * @param mixed $sut
48
     * @psalm-param T of mixed
49
     * @psalm-return FluentCheck<T>
50
     * @return BooleanCheck|GenericCheck|CallableCheck|CollectionCheck|ResponseCheck|ArrayCheck|DateTimeCheck|StringCheck|ExceptionCheck|FloatCheck|IntegerCheck
51
     *
52
     */
53 362
    public static function that($sut): FluentCheck
54
    {
55 362
        if (\is_object($sut)) {
56 148
            if (self::hasAssertionClass(\get_class($sut))) {
57 1
                return new self::$assertionClassesMap[\get_class($sut)]($sut);
58
            }
59 148
            foreach (\array_keys(self::$assertionClassesMap) as $class) {
60 148
                if ($sut instanceof $class) {
61 37
                    return new self::$assertionClassesMap[$class]($sut);
62
                }
63
            }
64
        }
65 346
        if (\is_callable($sut)) {
66 43
            return new self::$assertionClassesMap['callable']($sut);
67
        }
68 341
        if (\is_array($sut)) {
69 41
            return new self::$assertionClassesMap['array']($sut);
70
        }
71 324
        if (\is_iterable($sut)) {
72 2
            return new self::$assertionClassesMap['iterable']($sut);
73
        }
74 323
        $type = \gettype($sut);
75 323
        if (self::hasAssertionClass($type)) {
76 258
            return new self::$assertionClassesMap[$type]($sut);
77
        }
78 133
        return new GenericCheck($sut);
79
    }
80
81
    /**
82
     * @param callable $function
83
     *
84
     * @return CallableCheck
85
     */
86 34
    public static function thatCall(callable $function): CallableCheck
87
    {
88 34
        return self::that($function);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::that($function) returns the type Pitchart\Phlunit\Checks\GenericCheck which is incompatible with the type-hinted return Pitchart\Phlunit\Checks\CallableCheck.
Loading history...
89
    }
90
91
    /**
92
     * @param string $className
93
     * @param class-string $assertionClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
94
     */
95 1
    public static function registerChecksFor(string $className, string $assertionClass): void
96
    {
97 1
        self::$assertionClassesMap[$className] = $assertionClass;
98 1
    }
99
}
100