• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / whip / 7300735247

22 Dec 2023 02:07PM UTC coverage: 31.683% (+3.0%) from 28.713%
7300735247

push

github

web-flow
Merge pull request #157 from Yoast/move-to-namespace-v2

Move source classes to the `Yoast\WHIPv2` namespace

30 of 114 new or added lines in 18 files covered. (26.32%)

96 of 303 relevant lines covered (31.68%)

2.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.14
/src/VersionRequirement.php
1
<?php
2

3
namespace Yoast\WHIPv2;
4

5
use Yoast\WHIPv2\Exceptions\EmptyProperty;
6
use Yoast\WHIPv2\Exceptions\InvalidOperatorType;
7
use Yoast\WHIPv2\Exceptions\InvalidType;
8
use Yoast\WHIPv2\Exceptions\InvalidVersionComparisonString;
9
use Yoast\WHIPv2\Interfaces\Requirement;
10

11
/**
12
 * A value object containing a version requirement for a component version.
13
 */
14
class VersionRequirement implements Requirement {
15

16
        /**
17
         * The component name.
18
         *
19
         * @var string
20
         */
21
        private $component;
22

23
        /**
24
         * The component version.
25
         *
26
         * @var string
27
         */
28
        private $version;
29

30
        /**
31
         * The operator to use when comparing version.
32
         *
33
         * @var string
34
         */
35
        private $operator;
36

37
        /**
38
         * Requirement constructor.
39
         *
40
         * @param string $component The component name.
41
         * @param string $version   The component version.
42
         * @param string $operator  The operator to use when comparing version.
43
         */
44
        public function __construct( $component, $version, $operator = '=' ) {
×
45
                $this->validateParameters( $component, $version, $operator );
×
46

47
                $this->component = $component;
×
48
                $this->version   = $version;
×
49
                $this->operator  = $operator;
×
50
        }
51

52
        /**
53
         * Retrieves the component name defined for the requirement.
54
         *
55
         * @return string The component name.
56
         */
57
        public function component() {
30✔
58
                return $this->component;
30✔
59
        }
60

61
        /**
62
         * Gets the components version defined for the requirement.
63
         *
64
         * @return string
65
         */
66
        public function version() {
30✔
67
                return $this->version;
30✔
68
        }
69

70
        /**
71
         * Gets the operator to use when comparing version numbers.
72
         *
73
         * @return string The comparison operator.
74
         */
75
        public function operator() {
27✔
76
                return $this->operator;
27✔
77
        }
78

79
        /**
80
         * Creates a new version requirement from a comparison string.
81
         *
82
         * @param string $component        The component for this version requirement.
83
         * @param string $comparisonString The comparison string for this version requirement.
84
         *
85
         * @return VersionRequirement The parsed version requirement.
86
         *
87
         * @throws InvalidVersionComparisonString When an invalid version comparison string is passed.
88
         */
89
        public static function fromCompareString( $component, $comparisonString ) {
3✔
90

91
                $matcher = '`
2✔
92
                        (
93
                                >=?     # Matches >= and >.
94
                                |
95
                                <=?     # Matches <= and <.
96
                        )
97
                        ([^>=<\s]+) # Matches anything except >, <, =, and whitespace.
98
                `x';
2✔
99

100
                if ( ! \preg_match( $matcher, $comparisonString, $match ) ) {
3✔
101
                        throw new InvalidVersionComparisonString( $comparisonString );
3✔
102
                }
103

104
                $version  = $match[2];
×
105
                $operator = $match[1];
×
106

NEW
107
                return new VersionRequirement( $component, $version, $operator );
×
108
        }
109

110
        /**
111
         * Validates the parameters passed to the requirement.
112
         *
113
         * @param string $component The component name.
114
         * @param string $version   The component version.
115
         * @param string $operator  The operator to use when comparing version.
116
         *
117
         * @return void
118
         *
119
         * @throws EmptyProperty       When any of the parameters is empty.
120
         * @throws InvalidOperatorType When the $operator parameter is invalid.
121
         * @throws InvalidType         When any of the parameters is not of the expected type.
122
         */
123
        private function validateParameters( $component, $version, $operator ) {
21✔
124
                if ( empty( $component ) ) {
21✔
125
                        throw new EmptyProperty( 'Component' );
3✔
126
                }
127

128
                if ( ! \is_string( $component ) ) {
18✔
129
                        throw new InvalidType( 'Component', $component, 'string' );
3✔
130
                }
131

132
                if ( empty( $version ) ) {
15✔
133
                        throw new EmptyProperty( 'Version' );
3✔
134
                }
135

136
                if ( ! \is_string( $version ) ) {
12✔
137
                        throw new InvalidType( 'Version', $version, 'string' );
3✔
138
                }
139

140
                if ( empty( $operator ) ) {
9✔
141
                        throw new EmptyProperty( 'Operator' );
3✔
142
                }
143

144
                if ( ! \is_string( $operator ) ) {
6✔
145
                        throw new InvalidType( 'Operator', $operator, 'string' );
3✔
146
                }
147

148
                $validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );
3✔
149
                if ( ! \in_array( $operator, $validOperators, true ) ) {
3✔
150
                        throw new InvalidOperatorType( $operator, $validOperators );
3✔
151
                }
152
        }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc