Skip to content

Commit 43d04e0

Browse files
committed
Use strict parameter and return types; requires PHP >= 7.1
1 parent 6ba6397 commit 43d04e0

13 files changed

+99
-92
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Inspired by [Params::Validate](http://search.cpan.org/perldoc/Params::Validate)
66
You can use it to validate almost anything, but typically it can be used for (strictly) validating input from HTML forms, fields while reading CSV files, function parameters, etc.
77

88
### Requirements
9-
* PHP 5.4 or later
9+
* PHP 7.1 or later
1010

1111
### Installation
1212
Download or checkout from git, or install the packagist package cmanley/validate.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.4.0"
15+
"php": ">=7.1"
1616
},
1717
"autoload": {
1818
"psr-4": {

src/Exception/NamedValueException.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Contains the Validate\Exception\NamedValueException class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2018-2024, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
99
namespace Validate\Exception;
@@ -31,9 +31,8 @@ class NamedValueException extends ValueException {
3131
* @param string $check name of check that failed
3232
* @param mixed $value the value that isn't valid
3333
* @param string $message optional custom message
34-
* @param array $options
3534
*/
36-
public function __construct($name, $check, $value, $message = null) {
35+
public function __construct(string $name, string $check, $value, string $message = null) {
3736
$this->name = $name;
3837
$this->check = $check;
3938
$this->value = $value;
@@ -52,7 +51,7 @@ public function __construct($name, $check, $value, $message = null) {
5251
*
5352
* @return string|null
5453
*/
55-
public function getName() {
54+
public function getName(): ?string {
5655
return $this->name;
5756
}
5857
}

src/Exception/ValidationException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Contains the Validate\Exception\ValidationException class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2018-2024, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
99
namespace Validate\Exception;

src/Exception/ValueException.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Contains the Validate\Exception\ValueException class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2018-2024, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
99
namespace Validate\Exception;
@@ -29,9 +29,8 @@ class ValueException extends ValidationException {
2929
* @param string $check name of check that failed
3030
* @param mixed $value the value that isn't valid
3131
* @param string $message optional custom message
32-
* @param array $options
3332
*/
34-
public function __construct($check, $value, $message = null) {
33+
public function __construct(string $check, $value, string $message = null) {
3534
$this->check = $check;
3635
$this->value = $value;
3736
if (is_null($message)) {
@@ -49,7 +48,7 @@ public function __construct($check, $value, $message = null) {
4948
*
5049
* @return string
5150
*/
52-
public function getCheck() {
51+
public function getCheck(): string {
5352
return $this->check;
5453
}
5554

@@ -60,7 +59,7 @@ public function getCheck() {
6059
*
6160
* @return string|null
6261
*/
63-
public function getStringPlaceholderValue() {
62+
public function getStringPlaceholderValue(): ?string {
6463
if (is_scalar($this->value)) {
6564
if (is_bool($this->value)) {
6665
return $this->value ? 'true' : 'false';
@@ -91,7 +90,7 @@ public function getValue() {
9190
*
9291
* @return string
9392
*/
94-
public function getValueSimple() {
93+
public function getValueSimple(): string {
9594
if (is_bool($this->value)) {
9695
return $this->value ? 'true' : 'false';
9796
}

src/Spec.php

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Contains the Spec class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2016-2024, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
99
namespace Validate;
@@ -85,12 +85,12 @@ class Spec {
8585
*
8686
* The following options are supported:
8787
* <pre>
88-
* allow_empty : boolean, allow empty strings to be validated and pass 'optional' check.
88+
* allow_empty : bool, allow empty strings to be validated and pass 'optional' check.
8989
* before : Callback that takes a reference to the value as argument so that it can mutate it before validation. It may trigger validation failure by returning boolean false.
9090
* after : Callback that takes a reference to the value as argument so that it can mutate it after validation. It may trigger validation failure by returning boolean false.
9191
* default : Any non-null value; null arguments to validate() are replaced with this
92-
* optional : boolean, if true, then null values are allowed
93-
* trim : boolean, if true, then whitespace is trimmed off both ends of string values before validation.
92+
* optional : bool, if true, then null values are allowed
93+
* trim : bool, if true, then whitespace is trimmed off both ends of string values before validation.
9494
* description : Optional description that can be used by user code.
9595
* validation : Validation (constraints) object used to validate non-null values.
9696
* </pre>
@@ -103,7 +103,7 @@ class Spec {
103103
public function __construct(array $args = null) {
104104
if ($args) {
105105
$boolean_options = array('allow_empty', 'optional', 'trim');
106-
$unknown_args = array();
106+
$unknown_args = [];
107107
foreach ($args as $key => $value) {
108108
if ($key == 'validation') {
109109
if (!is_null($value)) {
@@ -137,7 +137,7 @@ public function __construct(array $args = null) {
137137
}
138138
# Process boolean options
139139
elseif (in_array($key, $boolean_options)) {
140-
$this->$key = (boolean) $value;
140+
$this->$key = (bool) $value;
141141
}
142142

143143
elseif (substr($key,0,1) === '_') {
@@ -167,12 +167,13 @@ public function __construct(array $args = null) {
167167
*
168168
* @throws \BadMethodCallException
169169
*/
170-
public function __get($key) {
170+
#public __get(string $name): mixed { # PHP8
171+
public function __get(string $name) {
171172
# TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why.
172173
$r = new \ReflectionObject($this);
173174
$p = null;
174175
try {
175-
$p = $r->getProperty($key);
176+
$p = $r->getProperty($name);
176177
}
177178
catch (\ReflectionException $e) {
178179
# snuff unknown properties with exception message 'Property x does not exist'
@@ -181,7 +182,7 @@ public function __get($key) {
181182
$p->setAccessible(true); # Allow access to non-public members.
182183
return $p->getValue($this);
183184
}
184-
throw new \BadMethodCallException('Attempt to read undefined property ' . get_class($this) . '->' . $key);
185+
throw new \BadMethodCallException('Attempt to read undefined property ' . get_class($this) . '->' . $name);
185186
}
186187

187188

@@ -190,7 +191,7 @@ public function __get($key) {
190191
*
191192
* @return bool
192193
*/
193-
public function allow_empty() {
194+
public function allow_empty(): bool {
194195
trigger_error('Method ' . __METHOD__ . ' is deprecated; read the ' . __FUNCTION__ . ' property instead', E_USER_DEPRECATED);
195196
return $this->allow_empty;
196197
}
@@ -199,7 +200,7 @@ public function allow_empty() {
199200
/**
200201
* Returns the value of the 'default' option as passed into the constructor.
201202
*
202-
* @return string|null
203+
* @return mixed
203204
*/
204205
public function getDefault() {
205206
trigger_error('Method ' . __METHOD__ . ' is deprecated; read the ' . __FUNCTION__ . ' property instead', E_USER_DEPRECATED);
@@ -212,7 +213,7 @@ public function getDefault() {
212213
*
213214
* @return string|null
214215
*/
215-
public function description() {
216+
public function description(): ?string {
216217
trigger_error('Method ' . __METHOD__ . ' is deprecated; read the ' . __FUNCTION__ . ' property instead', E_USER_DEPRECATED);
217218
return $this->description;
218219
}
@@ -221,7 +222,7 @@ public function description() {
221222
/**
222223
* Return the before option as passed in the constructor.
223224
*
224-
* @return bool
225+
* @return Callable
225226
*/
226227
public function before() {
227228
trigger_error('Method ' . __METHOD__ . ' is deprecated; read the ' . __FUNCTION__ . ' property instead', E_USER_DEPRECATED);
@@ -232,7 +233,7 @@ public function before() {
232233
/**
233234
* Return the after option as passed in the constructor.
234235
*
235-
* @return bool
236+
* @return Callable
236237
*/
237238
public function after() {
238239
trigger_error('Method ' . __METHOD__ . ' is deprecated; read the ' . __FUNCTION__ . ' property instead', E_USER_DEPRECATED);
@@ -278,7 +279,7 @@ public function validation() {
278279
*
279280
* @return string|null
280281
*/
281-
public function getLastFailure() {
282+
public function getLastFailure(): ?string {
282283
return $this->last_failure;
283284
}
284285

@@ -291,7 +292,7 @@ public function getLastFailure() {
291292
* @param mixed &$arg
292293
* @return bool
293294
*/
294-
public function validate(&$arg) {
295+
public function validate(&$arg): bool {
295296
if (is_string($arg)) {
296297
if ($this->trim) {
297298
$arg = trim($arg); # trim is multibyte safe
@@ -364,7 +365,7 @@ public function validate(&$arg) {
364365
* @param mixed &$arg
365366
* @throws Validate\Exception\ValueException
366367
*/
367-
public function validate_ex(&$arg) {
368+
public function validate_ex(&$arg): void {
368369
if (!$this->validate($arg)) {
369370
throw new ValueException($this->getLastFailure(), $arg);
370371
}

src/SpecCollection.php

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Contains the Validate\SpecCollection class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2016-2024, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
99
namespace Validate;
@@ -32,10 +32,10 @@ class SpecCollection implements \Countable, \IteratorAggregate, \ArrayAccess {
3232
* If boolean values are given, then these are converted into a Spec object with optional => !boolean.
3333
* If array values are given, then these are passed as arguments into the constructor of a new Spec object.
3434
*
35-
* @param array associative array of field name => Spec|boolean|array pairs
35+
* @param array associative array of field name => Spec|bool|array pairs
3636
* @throws InvalidArgumentException
3737
*/
38-
public function __construct(array $pairs = array()) {
38+
public function __construct(array $pairs = []) {
3939
foreach ($pairs as $key => &$value) {
4040
static::_checkKeyValuePair($key, $value);
4141
unset($value);
@@ -51,12 +51,12 @@ public function __construct(array $pairs = array()) {
5151
* @param bool $value
5252
* @throws InvalidArgumentException
5353
*/
54-
private static function _booleanToSpec($value) {
55-
$property = 'boolean_spec_' . var_export((boolean)$value, true);
54+
private static function _boolToSpec(bool $value): Spec {
55+
$property = 'boolean_spec_' . var_export((bool)$value, true);
5656
if (!static::$$property) {
57-
static::$$property = (new Spec(array(
57+
static::$$property = (new Spec([
5858
'optional' => !$value,
59-
)));
59+
]));
6060
}
6161
return static::$$property;
6262
}
@@ -69,7 +69,7 @@ private static function _booleanToSpec($value) {
6969
* @param mixed &$value
7070
* @throws InvalidArgumentException
7171
*/
72-
private static function _checkKeyValuePair($key, &$value) {
72+
private static function _checkKeyValuePair($key, &$value): void {
7373
if (!((is_string($key) && strlen($key)) || is_int($key))) {
7474
throw new \InvalidArgumentException('Only string or int keys are allowed');
7575
}
@@ -78,7 +78,7 @@ private static function _checkKeyValuePair($key, &$value) {
7878
$value = new Spec($value);
7979
}
8080
elseif (is_bool($value) || is_int($value)) {
81-
$value = static::_booleanToSpec($value);
81+
$value = static::_boolToSpec((bool)$value);
8282
}
8383
else {
8484
throw new \InvalidArgumentException('Array values must be NULL or boolean or array or an instance of Spec. Got ' . gettype($value) . " for $key");
@@ -91,9 +91,9 @@ private static function _checkKeyValuePair($key, &$value) {
9191
* Return count of items in collection.
9292
* Implements countable
9393
*
94-
* @return integer
94+
* @return int
9595
*/
96-
public function count() {
96+
public function count(): int {
9797
return count($this->pairs);
9898
}
9999

@@ -103,15 +103,16 @@ public function count() {
103103
*
104104
* @return ArrayIterator
105105
*/
106-
public function getIterator() {
106+
public function getIterator(): \ArrayIterator {
107107
return new \ArrayIterator($this->pairs); # Uses a copy of pairs; the caller can't mutate this.
108108
}
109109

110110

111111
/**
112112
* Implements ArrayAccess
113113
*/
114-
public function offsetSet($offset, $value) {
114+
#public function offsetSet(mixed $offset, mixed $value): void { # PHP8
115+
public function offsetSet($offset, $value): void {
115116
throw new \BadMethodCallException("Attempt to set value of key \"$offset\" on immutable instance of " . get_class($this));
116117
#static::_checkKeyValuePair($offset, $value);
117118
#$this->pairs[$offset] = $value;
@@ -123,15 +124,17 @@ public function offsetSet($offset, $value) {
123124
*
124125
* @return bool
125126
*/
126-
public function offsetExists($offset) {
127+
#public function offsetExists(mixed $offset): bool { # PHP8
128+
public function offsetExists($offset): bool {
127129
return array_key_exists($offset, $this->pairs);
128130
}
129131

130132

131133
/**
132134
* Implements ArrayAccess
133135
*/
134-
public function offsetUnset($offset) {
136+
#public offsetUnset(mixed $offset): void { # PHP8
137+
public function offsetUnset($offset): void {
135138
throw new \BadMethodCallException("Attempt to unset key \"$offset\" on immutable instance of " . get_class($this));
136139
#unset($this->pairs[$offset]);
137140
}
@@ -140,8 +143,10 @@ public function offsetUnset($offset) {
140143
/**
141144
* Implements ArrayAccess
142145
*
143-
* @return bool
146+
* @return mixed
144147
*/
148+
#[\ReturnTypeWillChange]
149+
#public function offsetGet(mixed $offset): mixed { # PHP8
145150
public function offsetGet($offset) {
146151
return array_key_exists($offset, $this->pairs) ? $this->pairs[$offset] : null;
147152
}
@@ -152,7 +157,7 @@ public function offsetGet($offset) {
152157
*
153158
* @return array
154159
*/
155-
public function toArray() {
160+
public function toArray(): array {
156161
return $this->pairs;
157162
}
158163

@@ -162,7 +167,7 @@ public function toArray() {
162167
*
163168
* @return array
164169
*/
165-
public function keys() {
170+
public function keys(): array {
166171
return array_keys($this->pairs);
167172
}
168173

0 commit comments

Comments
 (0)