Skip to content

Commit 66f952a

Browse files
authored
Add native type information (#17)
1 parent 28f7999 commit 66f952a

9 files changed

Lines changed: 46 additions & 45 deletions

File tree

.github/workflows/test.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
- 'high'
2121
- 'low'
2222
php:
23-
- '7.2'
24-
- '7.3'
25-
- '7.4'
2623
- '8.0'
2724

2825
steps:

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [3.0.0] - Unreleased
7+
### Summary
8+
This release now requires PHP 8
9+
10+
Breaking changes:
11+
- Trying to validate or evaluate an InputObject before the value was set will now throw an `Error` instead of a `BadMethodCallException`.
12+
If you encounter a property access before initialization error, this is the cause, and it indicates incorrect use of the library.
13+
14+
- `InputObject::validate(mixed $value): bool` now has the native parameter type; subclasses should do the same.
15+
616
## [2.2.0] - 2021-07-19
717
### Summary
818
This release focuses on supporting newer versions of PHP and minimizing external dependencies.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"sort-packages": true
2323
},
2424
"require": {
25-
"php": "^7.2 || ^8.0"
25+
"php": "^8.0"
2626
},
2727
"require-dev": {
2828
"phpstan/phpstan": "^0.12.32",

src/Containers/ParsedInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ParsedInput extends RawInput implements ArrayAccess
1818
{
1919
/**
20-
* @param array<array-key-, mixed> $data
20+
* @param array<array-key, mixed> $data
2121
*/
2222
public function __construct(array $data)
2323
{

src/Containers/RawInput.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88

99
class RawInput
1010
{
11-
/** @var mixed */
12-
private $data;
13-
/** @var bool */
14-
private $is_parsed = false;
15-
/** @var bool */
16-
private $is_validated = false;
11+
private mixed $data;
12+
private bool $isParsed = false;
13+
private bool $isValidated = false;
1714

1815
/**
1916
* @param mixed $raw
@@ -52,23 +49,23 @@ final protected function setData(array $data): self
5249
// mocked during unit tests
5350
public function isParsed(): bool
5451
{
55-
return $this->is_parsed;
52+
return $this->isParsed;
5653
}
5754

5855
public function isValidated(): bool
5956
{
60-
return $this->is_validated;
57+
return $this->isValidated;
6158
}
6259

6360
final protected function setIsParsed(bool $bool): self
6461
{
65-
$this->is_parsed = $bool;
62+
$this->isParsed = $bool;
6663
return $this;
6764
}
6865

6966
final protected function setIsValidated(bool $bool): self
7067
{
71-
$this->is_validated = $bool;
68+
$this->isValidated = $bool;
7269
return $this;
7370
}
7471
}

src/Exceptions/InputException.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ class InputException extends UnexpectedValueException
2020
/**
2121
* @var string[]
2222
*/
23-
private $missing = [];
23+
private array $missing = [];
24+
2425
/**
2526
* @var string[]
2627
*/
27-
private $invalid = [];
28+
private array $invalid = [];
29+
2830
/**
2931
* @var string[]
3032
*/
31-
private $unexpected = [];
33+
private array $unexpected = [];
3234

3335
public function __construct(int $code, array $errors = [])
3436
{

src/Objects/InputObject.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@
2424
*/
2525
abstract class InputObject
2626
{
27-
/** @var mixed */
28-
private $defaultValue;
29-
/** @var mixed */
30-
private $value;
31-
/** @var bool */
32-
private $valueWasSet = false; // false-like values can be valid, so explicitly track if the setter has been called
33-
/** @var ?bool */
34-
private $isValid;
27+
private mixed $defaultValue = null;
28+
29+
/**
30+
* isValid tracks both a) if the value has been validated, and b) if it's
31+
* actually valid.
32+
*/
33+
private ?bool $isValid = null;
34+
35+
private mixed $value;
36+
37+
/**
38+
* Extending classes must implement the `validate` method, accepting the
39+
* arbitrary value from the input being validated and returning a boolean
40+
* indicating if the value is valid.
41+
*/
42+
abstract protected function validate(mixed $value): bool;
3543

3644
public function __construct()
3745
{
@@ -78,29 +86,16 @@ final protected function getValue()
7886
* @param mixed $value value to validate
7987
* @return $this
8088
*/
81-
final public function setValue($value): self
89+
final public function setValue(mixed $value): self
8290
{
8391
$this->isValid = null;
8492
$this->value = $value;
85-
$this->valueWasSet = true;
8693
return $this;
8794
}
8895

89-
/**
90-
* @param mixed $value
91-
*/
92-
abstract protected function validate($value): bool;
93-
94-
/**
95-
* @return bool
96-
*/
9796
final public function isValid(): bool
9897
{
9998
if (null === $this->isValid) {
100-
if (!$this->valueWasSet) {
101-
throw new \BadMethodCallException("Value has not been set");
102-
}
103-
10499
$this->isValid = $this->validate($this->value);
105100
}
106101
return $this->isValid;

tests/Objects/InputObjectTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace Firehed\Input\Objects;
66

77
use BadMethodCallException;
8+
use Error;
89
use UnexpectedValueException;
910

1011
/**
1112
* @covers Firehed\Input\Objects\InputObject
1213
*/
1314
class InputObjectTest extends \PHPUnit\Framework\TestCase
1415
{
15-
/** @var InputObject */
16-
private $io;
16+
private InputObject $io;
1717

1818
public function setUp(): void
1919
{
@@ -50,7 +50,7 @@ public function testIsValidBad(): void
5050

5151
public function testIsValidNoValueThrows(): void
5252
{
53-
$this->expectException(BadMethodCallException::class);
53+
$this->expectException(Error::class);
5454
$this->io->isValid();
5555
}
5656

@@ -74,7 +74,7 @@ public function testEvaluateInvalidValue(): void
7474

7575
public function testEvaluateNoValue(): void
7676
{
77-
$this->expectException(BadMethodCallException::class);
77+
$this->expectException(Error::class);
7878
$this->io->evaluate();
7979
}
8080

tests/Objects/InputObjectTestFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class InputObjectTestFixture extends InputObject
88
{
99
public const MAGIC_FAIL = '198sjs $ a2/';
1010

11-
public function validate($value): bool
11+
public function validate(mixed $value): bool
1212
{
1313
return $value !== self::MAGIC_FAIL;
1414
}

0 commit comments

Comments
 (0)