Passed
Pull Request — master (#33)
by Melech
05:54 queued 02:40
created

Assert::getAssertions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Test\Assert\Asserters;
15
16
use Valkyrja\Test\Assert\Assert as Contract;
17
use Valkyrja\Test\Assert\Asserter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Test\Assert\Asserters\Asserter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Valkyrja\Test\Assert\Asserters\Asserter as AbstractAsserter;
19
use Valkyrja\Test\Assert\Asserters\Compare as CompareAsserter;
20
use Valkyrja\Test\Assert\Asserters\Exceptions as ExceptionsAsserter;
21
use Valkyrja\Test\Assert\Asserters\Str as StrAsserter;
22
use Valkyrja\Test\Assert\Compare;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Test\Assert\Asserters\Compare. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use Valkyrja\Test\Assert\Enums\AsserterName;
24
use Valkyrja\Test\Assert\Enums\ResultType;
25
use Valkyrja\Test\Assert\Exceptions;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Test\Assert\Asserters\Exceptions. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
use Valkyrja\Test\Assert\Str;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Test\Assert\Asserters\Str. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
use Valkyrja\Type\Support\Enum;
28
29
/**
30
 * Class Assert.
31
 *
32
 * @author Melech Mizrachi
33
 */
34
class Assert extends AbstractAsserter implements Contract
35
{
36
    /**
37
     * Asserter instances.
38
     *
39
     * @var Asserter[]
40
     */
41
    protected array $asserterInstances = [];
42
43
    /**
44
     * @param array<string, class-string<Asserter>> $asserters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string<Asserter>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<Asserter>>.
Loading history...
45
     */
46
    public function __construct(
47
        protected array $asserters = [],
48
    ) {
49
        $this->asserters = array_merge(
50
            [
51
                AsserterName::compare->name    => CompareAsserter::class,
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\AsserterName.
Loading history...
52
                AsserterName::exceptions->name => ExceptionsAsserter::class,
53
                AsserterName::string->name     => StrAsserter::class,
54
            ],
55
            $asserters
56
        );
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function compare(): Compare
63
    {
64
        return $this->__call(AsserterName::compare->name, []);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\AsserterName.
Loading history...
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function exceptions(): Exceptions
71
    {
72
        return $this->__call(AsserterName::exceptions->name, []);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\AsserterName.
Loading history...
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function string(): Str
79
    {
80
        return $this->__call(AsserterName::string->name, []);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\AsserterName.
Loading history...
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function withAsserters(array $asserters): void
87
    {
88
        $this->asserters = array_merge($this->asserters, $asserters);
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function getAssertions(): array
95
    {
96
        return $this->getAllAsserterResults(ResultType::assertions);
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function getErrors(): array
103
    {
104
        return $this->getAllAsserterResults(ResultType::errors);
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function getSuccesses(): array
111
    {
112
        return $this->getAllAsserterResults(ResultType::successes);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function getWarnings(): array
119
    {
120
        return $this->getAllAsserterResults(ResultType::warnings);
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function __get(string $name): mixed
127
    {
128
        if (Enum::isValidName(ResultType::class, $name)) {
129
            return match ($name) {
130
                ResultType::assertions->name => $this->getAssertions(),
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\ResultType.
Loading history...
131
                ResultType::errors->name     => $this->getErrors(),
132
                ResultType::successes->name  => $this->getSuccesses(),
133
                ResultType::warnings->name   => $this->getWarnings(),
134
            };
135
        }
136
137
        return $this->__call($name, []);
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function __call(string $name, array $arguments): mixed
144
    {
145
        return $this->asserterInstances[$name] ??= new $this->asserters[$name]();
146
    }
147
148
    /**
149
     * Get all the asserters' results by type.
150
     */
151
    protected function getAllAsserterResults(ResultType $type): array
152
    {
153
        $results = $this->{$type->name};
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Assert\Enums\ResultType.
Loading history...
154
155
        foreach ($this->asserterInstances as $asserter) {
156
            $results[] = $asserter->{$type->name};
157
        }
158
159
        return array_merge(...$results);
160
    }
161
}
162