Issues (26)

src/Checks/FloatCheck.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Pitchart\Phlunit\Checks;
4
5
use PHPUnit\Framework\Assert;
6
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
7
use Pitchart\Phlunit\Checks\Mixin\FluentChecks;
8
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...
9
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
10
11
/**
12
 * Class FloatCheck
13
 *
14
 * @package Pitchart\Phlunit\Checks
15
 *
16
 * @author Julien VITTE <[email protected]>
17
 *
18
 * @implements FluentCheck<double>
19
 */
20
class FloatCheck implements FluentCheck
21
{
22 1
    use WithMessage, TypeCheck, FluentChecks, ConstraintCheck;
23
24
    /**
25
     * @var float
26
     */
27
    private $value;
28
29
    /**
30
     * @var float
31
     */
32
    private $delta = 0.0;
33
34 12
    public function __construct(float $value)
35
    {
36 12
        $this->value = $value;
37 12
    }
38
39 2
    public function isEqualTo(float $expected): self
40
    {
41 2
        if ($this->delta) {
42 1
            Assert::assertEqualsWithDelta($expected, $this->value, $this->delta, $this->message);
43
        } else {
44 1
            Assert::assertSame($expected, $this->value, $this->message);
45
        }
46 2
        $this->resetMessage();
47 2
        return $this;
48
    }
49
50 2
    public function isNotEqualTo(float $expected): self
51
    {
52 2
        if ($this->delta) {
53 1
            Assert::assertNotEqualsWithDelta($expected, $this->value, $this->delta, $this->message);
54
        } else {
55 2
            Assert::assertNotSame($expected, $this->value, $this->message);
56
        }
57 2
        $this->resetMessage();
58 2
        return $this;
59
    }
60
61 1
    public function isEmpty(): self
62
    {
63 1
        Assert::assertEmpty($this->value, $this->message);
64 1
        $this->resetMessage();
65 1
        return $this;
66
    }
67
68 1
    public function isNotEmpty(): self
69
    {
70 1
        Assert::assertNotEmpty($this->value, $this->message);
71 1
        $this->resetMessage();
72 1
        return $this;
73
    }
74
75 1
    public function isGreaterThan(float $expected): self
76
    {
77 1
        Assert::assertGreaterThan($expected, $this->value, $this->message);
78 1
        $this->resetMessage();
79 1
        return $this;
80
    }
81
82 1
    public function isGreaterThanOrEqualTo(float $expected): self
83
    {
84 1
        Assert::assertGreaterThanOrEqual($expected, $this->value, $this->message);
85 1
        $this->resetMessage();
86 1
        return $this;
87
    }
88
89 1
    public function isLessThan(float $expected): self
90
    {
91 1
        Assert::assertLessThan($expected, $this->value, $this->message);
92 1
        $this->resetMessage();
93 1
        return $this;
94
    }
95
96 1
    public function isLessThanOrEqualTo(float $expected): self
97
    {
98 1
        Assert::assertLessThanOrEqual($expected, $this->value, $this->message);
99 1
        $this->resetMessage();
100 1
        return $this;
101
    }
102
103 1
    public function withDelta(float $delta): self
104
    {
105 1
        $this->delta = $delta;
106 1
        return $this;
107
    }
108
109 1
    public function strictly(): self
110
    {
111 1
        return $this->withDelta(0.0);
112
    }
113
}
114