Completed
Push — master ( d21468...5b2b8e )
by Joschi
02:16
created

AbstractMinMaxMediaCondition::matches()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
crap 3.7085
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Domain\Model\Css
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Respimgcss\Domain\Model\Css;
38
39
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Contract\LengthInterface;
42
use Jkphl\Respimgcss\Domain\Contract\RelativeLengthInterface;
43
use Jkphl\Respimgcss\Domain\Exceptions\InvalidArgumentException;
44
45
/**
46
 * Abstract min/max media condition
47
 *
48
 * @package    Jkphl\Respimgcss
49
 * @subpackage Jkphl\Respimgcss\Domain\Model\Css
50
 */
51
abstract class AbstractMinMaxMediaCondition extends MediaCondition implements CssMinMaxMediaConditionInterface
52
{
53
    /**
54
     * Property name
55
     *
56
     * @var string
57
     */
58
    const PROPERTY = 'none';
59
    /**
60
     * Property modifier
61
     *
62
     * @var string
63
     */
64
    protected $modifier = self::EQ;
65
    /**
66
     * Property value
67
     *
68
     * @var AbsoluteLengthInterface|RelativeLengthInterface
69
     */
70
    protected $value;
71
72
    /**
73
     * Min/Max CSS media condition constructor
74
     *
75
     * @param AbsoluteLengthInterface|RelativeLengthInterface $value Property value
76
     * @param string $modifier                                       Condition modifier
77
     *
78
     */
79 18
    public function __construct(LengthInterface $value, string $modifier = self::EQ)
80
    {
81 18
        parent::__construct(static::PROPERTY, $value);
82
83
        if (
84 18
            ($modifier !== self::EQ)
85 18
            && ($modifier !== self::MIN)
86 18
            && ($modifier !== self::MAX)
87
        ) {
88 1
            throw new InvalidArgumentException(
89 1
                sprintf(InvalidArgumentException::INVALID_CSS_CONDITION_MODIFIER_STR, $modifier),
90 1
                InvalidArgumentException::INVALID_CSS_CONDITION_MODIFIER
91
            );
92
        }
93
94 17
        $this->modifier = $modifier;
95 17
    }
96
97
    /**
98
     * Return the property modifier
99
     *
100
     * @return string Property modifier
101
     */
102 8
    public function getModifier(): string
103
    {
104 8
        return $this->modifier;
105
    }
106
107
    /**
108
     * Return the property value
109
     *
110
     * @return AbsoluteLengthInterface|RelativeLengthInterface Property value
111
     */
112 9
    public function getValue()
113
    {
114 9
        return $this->value;
115
    }
116
117
    /**
118
     * Test whether this condition matches a value
119
     *
120
     * @param float $value Value
121
     *
122
     * @return bool Successful match
123
     */
124 1
    public function matches(float $value): bool
125
    {
126 1
        $conditionValue = $this->value->getValue();
0 ignored issues
show
Bug introduced by
The call to Jkphl\Respimgcss\Domain\...thInterface::getValue() has too few arguments starting with viewport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        /** @scrutinizer ignore-call */ 
127
        $conditionValue = $this->value->getValue();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
127
128 1
        if ($this->modifier === self::MIN) {
129 1
            return $value >= $conditionValue;
130
        }
131
132
        if ($this->modifier === self::MAX) {
133
            return $value <= $conditionValue;
134
        }
135
136
        return $value == $conditionValue;
137
    }
138
}