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

Yoast / whip / 6985375056

24 Nov 2023 10:53PM UTC coverage: 28.713% (+1.2%) from 27.5%
6985375056

push

github

web-flow
Merge pull request #152 from Yoast/JRF/ghactions-switch-to-coveralls-reporter

GH Actions: switch to Coveralls action runner to upload reports

87 of 303 relevant lines covered (28.71%)

2.62 hits per line

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

77.14
/src/Whip_VersionRequirement.php
1
<?php
2
/**
3
 * WHIP libary file.
4
 *
5
 * @package Yoast\WHIP
6
 */
7

8
/**
9
 * A value object containing a version requirement for a component version.
10
 */
11
class Whip_VersionRequirement implements Whip_Requirement {
12

13
        /**
14
         * The component name.
15
         *
16
         * @var string
17
         */
18
        private $component;
19

20
        /**
21
         * The component version.
22
         *
23
         * @var string
24
         */
25
        private $version;
26

27
        /**
28
         * The operator to use when comparing version.
29
         *
30
         * @var string
31
         */
32
        private $operator;
33

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

44
                $this->component = $component;
×
45
                $this->version   = $version;
×
46
                $this->operator  = $operator;
×
47
        }
48

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

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

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

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

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

97
                if ( ! preg_match( $matcher, $comparisonString, $match ) ) {
3✔
98
                        throw new Whip_InvalidVersionComparisonString( $comparisonString );
3✔
99
                }
100

101
                $version  = $match[2];
×
102
                $operator = $match[1];
×
103

104
                return new Whip_VersionRequirement( $component, $version, $operator );
×
105
        }
106

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

125
                if ( ! is_string( $component ) ) {
18✔
126
                        throw new Whip_InvalidType( 'Component', $component, 'string' );
3✔
127
                }
128

129
                if ( empty( $version ) ) {
15✔
130
                        throw new Whip_EmptyProperty( 'Version' );
3✔
131
                }
132

133
                if ( ! is_string( $version ) ) {
12✔
134
                        throw new Whip_InvalidType( 'Version', $version, 'string' );
3✔
135
                }
136

137
                if ( empty( $operator ) ) {
9✔
138
                        throw new Whip_EmptyProperty( 'Operator' );
3✔
139
                }
140

141
                if ( ! is_string( $operator ) ) {
6✔
142
                        throw new Whip_InvalidType( 'Operator', $operator, 'string' );
3✔
143
                }
144

145
                $validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );
3✔
146
                if ( ! in_array( $operator, $validOperators, true ) ) {
3✔
147
                        throw new Whip_InvalidOperatorType( $operator, $validOperators );
3✔
148
                }
149
        }
150
}
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