Skip to content

Commit 149783b

Browse files
committed
use PHP7 language features
This is the end of the v1.x branch because this commit introduces BC-breaking changes. Most importantly, the `isValid()` method is no longer a stub in the `AbstractValue` base class; it's now an `abstract static` method, implicitly forcing users of the class to implement that method without the need for a `NotImplementedException`. Additionally, the AbstractValue methods now have return types.
1 parent db88ecb commit 149783b

11 files changed

+54
-100
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: php
22

33
php:
4-
# - '5.6'
54
- '7.0'
65
- '7.1'
76
- '7.2'
7+
- '7.3'
88

99
before_install:
1010
- composer self-update

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v2.0.0 (2019-10)
2+
3+
* Use PHP 7 language features
4+
(breaks PHP 5.3 compatibility)
5+
16
v1.1.2 (2019-10-23)
27
* package cleanup
38

README.md

+22-30
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class OddNumber
2222
{
2323

2424
// The base class requires this boolean test method:
25-
public static function IsValid ($input) {
25+
public static function isValid ($input): bool {
2626
return (is_int($input) && ($input % 2) === 1);
2727
}
2828

@@ -50,16 +50,14 @@ Or insert this into your project's `composer.json` file:
5050

5151
```js
5252
"require": {
53-
"mle86/value": "^1.0"
53+
"mle86/value": "^2.0.0"
5454
}
5555
```
5656

5757

5858
# Minimum PHP version:
5959

60-
* PHP 5.4 is needed for the `AbstractSerializableValue` class, as it uses the `JsonSerializable` interface.
61-
62-
* PHP 5.3 is sufficient for the rest (the `Value` interface and `AbstractValue` base class).
60+
PHP 7.0
6361

6462

6563
# Classes and interfaces:
@@ -84,80 +82,74 @@ This immutable class wraps a single value per instance.
8482
The constructor enforces validity checks on the input value.
8583
Therefore, every class instance's wrapped value can be considered valid.
8684

87-
The validity checks are located in the IsValid class method which all
85+
The validity checks are located in the isValid class method which all
8886
subclasses must implement. It is a class method to allow validity checks
8987
of external values without wrapping them in an instance.
9088

9189

9290
* <code>public function <b>\_\_construct</b> ($raw\_value)</code>
9391

94-
The constructor uses the subclass' `IsValid` method to test its input argument.
92+
The constructor uses the `isValid` class method to test its input argument.
9593
Valid values are stored in the new instance, invalid values cause an `InvalidArgumentException` to be thrown.
9694
Other instances of the same class are always considered valid (*re-wrapping*).
9795

98-
* <code>public static function <b>IsValid</b> ($test\_value)</code>
96+
* <code>public static function <b>isValid</b> ($test\_value): bool</code>
9997

100-
Checks the validity of a raw value. If it returns true, a new object can be instantiated with the same value.
98+
Checks the validity of a raw value.
99+
If it returns true, a new object can be instantiated with that value.
101100
Implement this in every subclass!
102-
In the base class implementation, it simply throws a `NotImplementedException`.
103101

104102
* <code>final public function <b>value</b> ()</code>
105103

106104
Returns the object's wrapped initializer value.
107105

108106
* <code>final public function <b>equals</b> ($test\_value)</code>
109107

108+
Equality test.
110109
This method performs an equality check on other instances or raw values.
111110
Objects are considered equal if and only if they are instances of the same subclass and carry the same `value()`.
112111
All other values are considered equal if and only if they are identical (`===`) to the current objects's `value()`.
113112

114-
* <code>final public static function <b>Wrap</b> (&$value)</code>
113+
* <code>final public static function <b>wrap</b> (&$value)</code>
115114

116115
Replaces a value (by-reference) with instance wrapping that value.
117-
This means of course that the call will fail with an `InvalidArgumentException` if the input value fails the subclass' `IsValid` check.
116+
This means of course that the call will fail with an `InvalidArgumentException` if the input value fails the subclass' `isValid` check.
118117
If the value already is an instance, it won't be replaced.
119118

120-
* <code>final public static function <b>WrapOrNull</b> (&$value)</code>
119+
* <code>final public static function <b>wrapOrNull</b> (&$value)</code>
121120

122-
Like `Wrap()`, but won't change `NULL` values.
121+
Like `wrap()`, but won't change `null` values.
123122

124-
* <code>final public static function <b>WrapArray</b> (array &$array)</code>
123+
* <code>final public static function <b>wrapArray</b> (array &$array): array</code>
125124

126125
Will replace all values in an array with instances.
127126
The array will only be altered (by-reference) if all its values are valid.
128127
Array keys will be preserved.
129128

130-
* <code>final public static function <b>WrapOrNullArray</b> (array &$array)</code>
129+
* <code>final public static function <b>wrapOrNullArray</b> (array &$array): array</code>
131130

132-
Will replace all non-`NULL` values in an array with instances.
133-
The array will only be changed (by-reference) if all its values are valid (or `NULL`).
131+
Will replace all non-`null` values in an array with instances.
132+
The array will only be changed (by-reference) if all its values are valid (or `null`).
134133
Array keys will be preserved.
135134

136135

137136
## AbstractSerializableValue
138137

139138
This extension of `AbstractValue` provides easy serializability for the Value objects.
140-
It implements the PHP 5.4 [JsonSerializable](https://php.net/manual/class.jsonserializable.php) interface.
139+
It implements the [JsonSerializable](https://php.net/manual/class.jsonserializable.php) interface.
141140

142-
* <code>public function <b>\_\_toString</b> ()</code>
141+
* <code>public function <b>\_\_toString</b> (): string</code>
143142

144-
Returns the wrapped value –
145-
like `value()`, but with an explicit `(string)` typecast.
146-
This allows string concatenation of Value objects.
143+
Returns the wrapped value like `value()`, but with an explicit
144+
`string` typecast. This allows string concatenation of Value objects.
147145

148146
* <code>public function <b>jsonSerialize</b> ()</code>
149147

150148
Returns the wrapped value –
151149
like `value()`.
152-
This enables [json_encode()](https://secure.php.net/json_encode) to encode the Value object.
150+
This enables [json_encode()](https://secure.php.net/json_encode) to encode the object.
153151

154152

155153
## InvalidArgumentException
156154

157155
An empty extension of PHP's `InvalidArgumentException`.
158-
159-
160-
## NotImplementedException
161-
162-
An empty extension of PHP's `ErrorException`.
163-

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929

3030
"require": {
31-
"php": ">=5.4.0"
31+
"php": ">=7.0.0"
3232
},
3333

3434
"scripts": {

src/AbstractSerializableValue.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* This extension of AbstractValue provides easy serializability
7-
* for the Value objects. It implements the PHP 5.4 JsonSerializable interface.
7+
* for the Value objects. It implements the JsonSerializable interface.
88
*
99
* @author Maximilian Eul
1010
* @link https://github.com/mle86/php-value
@@ -18,10 +18,8 @@ abstract class AbstractSerializableValue extends AbstractValue implements \JsonS
1818
*
1919
* (The typecast is necessary to prevent type mismatch errors
2020
* for number-wrapping classes.)
21-
*
22-
* @return string
2321
*/
24-
public function __toString()
22+
public function __toString(): string
2523
{
2624
return (string)$this->value();
2725
}

src/AbstractValue.php

+20-26
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131
abstract class AbstractValue implements Value
3232
{
3333

34+
/**
35+
* Checks the validity of a raw value.
36+
*
37+
* If this method returns true,
38+
* the constructor will accept that value.
39+
*
40+
* Always include an `if ($testValue instanceof static) { return true; }`
41+
* check, as already-wrapped values are always considered valid!
42+
*
43+
* @param mixed|static $testValue
44+
* @return bool
45+
*/
46+
abstract public static function isValid($testValue): bool;
47+
48+
3449
/**
3550
* This is the one value which this class wraps.
3651
* It must only be written to in the class constructor.
@@ -60,7 +75,7 @@ final public function value()
6075

6176

6277
/**
63-
* The constructor uses the subclass' {@see isValid} method to test its input
78+
* The constructor uses the {@see isValid} class method to test its input
6479
* argument. Valid values are stored in the new instance, invalid values
6580
* cause an InvalidArgumentException to be thrown.
6681
* Other instance of the same class are always considered valid (re-wrapping).
@@ -94,32 +109,11 @@ public function __construct($rawValue)
94109
? "'{$rawValue}'"
95110
: gettype($rawValue);
96111

97-
throw new InvalidArgumentException("not a valid " . get_called_class() . ": {$input}");
112+
throw new InvalidArgumentException("not a valid " . static::class . ": {$input}");
98113
}
99114
}
100115

101116

102-
/** @noinspection PhpDocMissingThrowsInspection */
103-
/**
104-
* Checks the validity of a raw value.
105-
*
106-
* If this method returns true,
107-
* the constructor will accept that value.
108-
*
109-
* Always include an 'if ($testValue instanceof static) { return true; }'
110-
* check, as already-wrapped values are always considered valid!
111-
*
112-
* @param mixed|static $testValue
113-
* @return bool
114-
*/
115-
public static function isValid(
116-
/** @noinspection PhpUnusedParameterInspection */
117-
$testValue
118-
) {
119-
throw new NotImplementedException(get_called_class() . "::isValid not implemented!");
120-
}
121-
122-
123117
/**
124118
* Equality test.
125119
*
@@ -131,7 +125,7 @@ public static function isValid(
131125
* @param mixed|static $testValue
132126
* @return bool
133127
*/
134-
final public function equals($testValue)
128+
final public function equals($testValue): bool
135129
{
136130
if ($testValue instanceof static) {
137131
// It's an instance of the same class. Compare the wrapped values:
@@ -189,7 +183,7 @@ final public static function wrapOrNull(&$value)
189183
* @param mixed[]|static[] $array
190184
* @return static[]
191185
*/
192-
final public static function wrapArray(array &$array)
186+
final public static function wrapArray(array &$array): array
193187
{
194188
$arrayCopy = $array;
195189
foreach ($arrayCopy as &$value) {
@@ -214,7 +208,7 @@ final public static function wrapArray(array &$array)
214208
* @param mixed[]|static[]|null[] $array
215209
* @return static[]|null[]
216210
*/
217-
final public static function wrapOrNullArray(array &$array)
211+
final public static function wrapOrNullArray(array &$array): array
218212
{
219213
$arrayCopy = $array;
220214
foreach ($arrayCopy as &$value) {

src/NotImplementedException.php

-8
This file was deleted.

test/01-IncompleteClassTest.php

-27
This file was deleted.

test/Helpers/TestSWrapper6.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class TestSWrapper6 extends AbstractSerializableValue
1111
{
1212

13-
public static function isValid ($test) {
13+
public static function isValid ($test): bool {
1414
if ($test instanceof self) {
1515
return true;
1616
}

test/Helpers/TestWrapper4.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class TestWrapper4 extends AbstractValue
1111
{
1212

13-
public static function isValid($test)
13+
public static function isValid($test): bool
1414
{
1515
if ($test instanceof self) {
1616
return true;

test/Helpers/TestWrapper9.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class TestWrapper9 extends AbstractValue
1111
{
1212

13-
public static function isValid($test)
13+
public static function isValid($test): bool
1414
{
1515
if ($test instanceof self) {
1616
return true;

0 commit comments

Comments
 (0)