Issues (26)

src/Checks/GenericCheck.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Checks;
5
6
use PHPUnit\Framework\Assert;
7
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
8
use Pitchart\Phlunit\Checks\Mixin\FluentChecks;
9
use Pitchart\Phlunit\Checks\Mixin\TypeCheck;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pitchart\Phlunit\Checks\TypeCheck. 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...
10
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
11
12
class GenericCheck implements FluentCheck
13
{
14 1
    use TypeCheck, FluentChecks, ConstraintCheck, WithMessage;
15
16
    /**
17
     * @var mixed
18
     */
19
    private $value;
20
21
    /**
22
     * GenericCheck constructor.
23
     *
24
     * @param mixed $value
25
     */
26 133
    public function __construct($value)
27
    {
28 133
        $this->value = $value;
29 133
    }
30
31
    /**
32
     * @param mixed $expected
33
     *
34
     * @return GenericCheck
35
     */
36 34
    public function isEqualTo($expected): self
37
    {
38 34
        Assert::assertSame($expected, $this->value, $this->message);
39 34
        $this->resetMessage();
40 34
        return $this;
41
    }
42
43
    /**
44
     * @param mixed $expected
45
     *
46
     * @return GenericCheck
47
     */
48 2
    public function isNotEqualTo($expected): self
49
    {
50 2
        Assert::assertNotEquals($expected, $this->value, $this->message);
51 2
        $this->resetMessage();
52 2
        return $this;
53
    }
54
55 2
    public function isEmpty(): self
56
    {
57 2
        Assert::assertEmpty($this->value, $this->message);
58 2
        $this->resetMessage();
59 2
        return $this;
60
    }
61
62 1
    public function isNotEmpty(): self
63
    {
64 1
        Assert::assertNotEmpty($this->value, $this->message);
65 1
        $this->resetMessage();
66 1
        return $this;
67
    }
68
}
69