Skip to content

Improved types for RunnerConfig.php#1071

Merged
dantleech merged 1 commit intophpbench:masterfrom
aivchen:phpstan-runner-config
Dec 21, 2023
Merged

Improved types for RunnerConfig.php#1071
dantleech merged 1 commit intophpbench:masterfrom
aivchen:phpstan-runner-config

Conversation

@aivchen
Copy link
Contributor

@aivchen aivchen commented Dec 20, 2023

No description provided.

@dantleech
Copy link
Member

dantleech commented Dec 21, 2023

Why not set the default to empty array as you originally suggested?

@aivchen
Copy link
Contributor Author

aivchen commented Dec 21, 2023

Because I found a solution not breaking B/C.

@dantleech
Copy link
Member

Is it a B/C break? Passing NULL would have resulted in a crash?

@aivchen
Copy link
Contributor Author

aivchen commented Dec 21, 2023

Seems I'm confused a bit. Let's see what we have.

Initial method (from master branch):

public function getIterations($default = null): array
{
return $this->iterations ?: $default;
}

And if I call getIterations without params I'll get TypeError as this method must return array:

$myConfig = RunnerConfig::create();
$myConfig->getIterations(); // Uncaught TypeError: PhpBench\Benchmark\RunnerConfig::getIterations(): Return value must be of type array, null returned

In #1069 I suggested this solution:

    public function getIterations(array $default = []): array
    {
        return $this->iterations ?: $default;
    }

But it's breaks B/C as some user can have the following code:

$myConfig = RunnerConfig::create();
$myConfig->getIterations(null); // Uncaught TypeError: PhpBench\Benchmark\RunnerConfig::getIterations(): Argument #1 ($default) must be of type array, null given

So to keep B/C we must not change method signature. In this PR we just check the value of $default param. And if $default === null we set it to an empty array:

public function getIterations($default = null): array
{
$default ??= [];
return $this->iterations ?: $default;
}

As you see the signature is not changed in comparing with master branch.

And this code will keep start working:

$myConfig = RunnerConfig::create();
$myConfig->getIterations();
$myConfig->getIterations(null);

On the other hand, users couldn't have the code like this as they would get TypeError. So in theory we can consider this case as a bug and use the solution from #1069. What do you think?

@dantleech
Copy link
Member

dantleech commented Dec 21, 2023

yes, I think B/C is only if they could depend on this behavor, in theory they could depend on the runtime error, but that would be truly horrifying (insert XKCD here). It's a bug fix 👍

@aivchen aivchen force-pushed the phpstan-runner-config branch from 603f3de to 964686d Compare December 21, 2023 09:33
@aivchen
Copy link
Contributor Author

aivchen commented Dec 21, 2023

Fixed:)

@dantleech dantleech merged commit 0bca981 into phpbench:master Dec 21, 2023
@dantleech
Copy link
Member

thanks!

@aivchen aivchen deleted the phpstan-runner-config branch December 21, 2023 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants