<p>This passed PHPStan level 8, but these are the errors I get when running level 9:</p>
<pre><code class="language-plain">------ ------------------------------------------------------------------------------------------------------- Line versa
62Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
72Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
73Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
84Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
85Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
94Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
95Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
104Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
105Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
119Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
120Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
------ ------------------------------------------------------------------------------------------------------- [ERROR] Found 12 errors
</code></pre>
<p>The issue is that <code>$input->getOption()</code> from Symfony's <code>InputInterface</code> returns <code>mixed</code> - even though it always returns <code>null</code> or a string.</p>
<p>If I did <code>--working-dir 2</code>, it would return the string <code>"2"</code>.</p>
<p>An empty string throws an error, and an empty value (if there are no extra arguments) returns <code>null</code>.</p>
<p>So, I know <code>$workingDir</code> will always be a string and <code>$extraArgs</code> will either a string or <code>null</code>.</p>
<h2 id="passing-level-8">Passing level 8</h2>
<p>To pass level 8, PHPStan needs to be sure the variables are what I think they are.</p>
<p>Here's the code I can use to get it to pass:</p>
<p>By using <code>assert()</code> and throwing an Exception if the condition fails, PHPStan is now happy with the code and my code passes level 9.</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>Although the extra code gets PHPStan to pass, it it worth it?</p>
<p>Is this extra code adding value? Does it make the code more readable or is it likely to prevent a bug?</p>
<p>In this case, I know the value will always be the type I expect.</p>
<p>I can work around this using a baseline or annotations for PHPStan to ignore these lines, <strong>or is level 8 good enough for this project</strong>?</p>
<p>Similar to 100% test coverage, is aiming for the highest PHPStan level an objective to be met, or is enough value being added added by the lower level?</p>
<p>Which level is right for you and this codebase?</p>
<p>This passed PHPStan level 8, but these are the errors I get when running level 9:</p>
<pre><code class="language-plain">------ ------------------------------------------------------------------------------------------------------- Line versa
62Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
72Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
73Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
84Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
85Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
94Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
95Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
104Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
105Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
119Parameter $extraArgs of static method App\Process\Process::create() expects string|null, mixed given.
120Parameter $workingDir of static method App\Process\Process::create() expects string, mixed given.
------ ------------------------------------------------------------------------------------------------------- [ERROR] Found 12 errors
</code></pre>
<p>The issue is that <code>$input->getOption()</code> from Symfony's <code>InputInterface</code> returns <code>mixed</code> - even though it always returns <code>null</code> or a string.</p>
<p>If I did <code>--working-dir 2</code>, it would return the string <code>"2"</code>.</p>
<p>An empty string throws an error, and an empty value (if there are no extra arguments) returns <code>null</code>.</p>
<p>So, I know <code>$workingDir</code> will always be a string and <code>$extraArgs</code> will either a string or <code>null</code>.</p>
<h2 id="passing-level-8">Passing level 8</h2>
<p>To pass level 8, PHPStan needs to be sure the variables are what I think they are.</p>
<p>Here's the code I can use to get it to pass:</p>
<p>By using <code>assert()</code> and throwing an Exception if the condition fails, PHPStan is now happy with the code and my code passes level 9.</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>Although the extra code gets PHPStan to pass, it it worth it?</p>
<p>Is this extra code adding value? Does it make the code more readable or is it likely to prevent a bug?</p>
<p>In this case, I know the value will always be the type I expect.</p>
<p>I can work around this using a baseline or annotations for PHPStan to ignore these lines, <strong>or is level 8 good enough for this project</strong>?</p>
<p>Similar to 100% test coverage, is aiming for the highest PHPStan level an objective to be met, or is enough value being added added by the lower level?</p>
<p>Which level is right for you and this codebase?</p>