Commit 16fc102f authored by Jan Kuchař's avatar Jan Kuchař

strictness: enum checks if all values called parent constructor

parent 71079e17
Pipeline #14408 failed with stages
in 36 seconds
......@@ -2,3 +2,12 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/grifart/phpstan-oneline/config.neon
parameters:
ignoreErrors:
# for phpstan 0.11
# -
# message: '#Strict comparison using === between int\\|string and null will always evaluate to false#'
# path: src/Internal/Meta.php
- '#Strict comparison using === between int\\|string and null will always evaluate to false#'
\ No newline at end of file
......@@ -51,6 +51,13 @@ final class Meta
foreach($values as $value) {
$scalar = $value->toScalar();
if ($scalar === NULL) {
throw new UsageException(
"Parent constructor has not been called while constructing one of {$this->getClass()} enum values."
);
}
if (isset($scalarToValues[$scalar])) {
throw new UsageException('You have provided duplicated scalar values.');
}
......
<?php declare(strict_types=1);
use Grifart\Enum\UsageException;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
/**
* @method static ForgottenConstructorCall VALUE1()
*/
abstract class ForgottenConstructorCall extends \Grifart\Enum\Enum
{
private const VALUE1 = 1;
protected static function provideInstances(): array
{
return [
new class extends ForgottenConstructorCall {
/** @noinspection PhpMissingParentConstructorInspection */
protected function __construct() {
// no parent call
}
},
];
}
}
Assert::exception(
function() {
ForgottenConstructorCall::VALUE1();
},
UsageException::class,
'Parent constructor has not been called while constructing one of ForgottenConstructorCall enum values.'
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment