"value":"\n <p>Today, whilst working on <a href=\"\/daily\/2024\/02\/19\/introducing-versa\">Versa<\/a>, I was experimenting with different PHPStan levels.<\/p>\n\n<p>Level 1 is the least strict level, and applies the fewest rules and returns the fewest results.<\/p>\n\n<p>As you increase the level, the stricter PHPStan is.<\/p>\n\n<h2 id=\"levelling-up-to-9\">Levelling up to 9<\/h2>\n\n<p>Here is the code to get the values of the <code>--extra-args<\/code> and <code>--working-dir<\/code> options:<\/p>\n\n<pre><code class=\"language-php\">$extraArgs = $input->getOption('extra-args');\n$workingDir = $input->getOption('working-dir');\n<\/code><\/pre>\n\n<p>This passed PHPStan level 8, but these are the errors I get when running level 9:<\/p>\n\n<pre><code class=\"language-plain\">------ -------------------------------------------------------------------------------------------------------\n Line versa\n------ -------------------------------------------------------------------------------------------------------\n 61 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 62 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 72 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 73 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 84 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 85 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 94 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 95 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 104 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 105 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 119 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 120 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n------ -------------------------------------------------------------------------------------------------------\n\n[ERROR] Found 12 errors\n<\/code><\/pre>\n\n<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>\n\n<p>If I did <code>--working-dir 2<\/code>, it would return the string <code>\"2\"<\/code>.<\/p>\n\n<p>An empty string throws an error, and an empty value (if there are no extra arguments) returns <code>null<\/code>.<\/p>\n\n<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>\n\n<h2 id=\"passing-level-8\">Passing level 8<\/h2>\n\n<p>To pass level 8, PHPStan needs to be sure the variables are what I think they are.<\/p>\n\n<p>Here's the code I can use to get it to pass:<\/p>\n\n<pre><code class=\"language-php\">$extraArgs = $input->getOption('extra-args');\n$workingDir = $input->getOption('working-dir');\nassert(is_string($extraArgs) || is_null($extraArgs));\nassert(is_string($workingDir));\n<\/code><\/pre>\n\n<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>\n\n<h2 id=\"here%27s-the-thing\">Here'sthething<\/h2>\n\n<p>AlthoughtheextracodegetsPHPStantopass,ititworthit?<\/p>\n\n<p>Isthisextracodeaddingvalue?Doesitmakethecodemorereadableorisitlikelytopreventabug?<\/p>\n\n<p>Inthiscase,IknowthevaluewillalwaysbethetypeIexpect.<\/p>\n\n<p>Icanworkaroundthisusingabaselineorannotations
"processed":"\n <p>Today, whilst working on <a href=\"http:\/\/default\/daily\/2024\/02\/19\/introducing-versa\">Versa<\/a>, I was experimenting with different PHPStan levels.<\/p>\n\n<p>Level 1 is the least strict level, and applies the fewest rules and returns the fewest results.<\/p>\n\n<p>As you increase the level, the stricter PHPStan is.<\/p>\n\n<h2 id=\"levelling-up-to-9\">Levelling up to 9<\/h2>\n\n<p>Here is the code to get the values of the <code>--extra-args<\/code> and <code>--working-dir<\/code> options:<\/p>\n\n<pre><code class=\"language-php\">$extraArgs = $input->getOption('extra-args');\n$workingDir = $input->getOption('working-dir');\n<\/code><\/pre>\n\n<p>This passed PHPStan level 8, but these are the errors I get when running level 9:<\/p>\n\n<pre><code class=\"language-plain\">------ -------------------------------------------------------------------------------------------------------\n Line versa\n------ -------------------------------------------------------------------------------------------------------\n 61 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 62 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 72 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 73 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 84 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 85 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 94 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 95 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 104 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 105 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n 119 Parameter $extraArgs of static method App\\Process\\Process::create() expects string|null, mixed given.\n 120 Parameter $workingDir of static method App\\Process\\Process::create() expects string, mixed given.\n------ -------------------------------------------------------------------------------------------------------\n\n[ERROR] Found 12 errors\n<\/code><\/pre>\n\n<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>\n\n<p>If I did <code>--working-dir 2<\/code>, it would return the string <code>\"2\"<\/code>.<\/p>\n\n<p>An empty string throws an error, and an empty value (if there are no extra arguments) returns <code>null<\/code>.<\/p>\n\n<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>\n\n<h2 id=\"passing-level-8\">Passing level 8<\/h2>\n\n<p>To pass level 8, PHPStan needs to be sure the variables are what I think they are.<\/p>\n\n<p>Here's the code I can use to get it to pass:<\/p>\n\n<pre><code class=\"language-php\">$extraArgs = $input->getOption('extra-args');\n$workingDir = $input->getOption('working-dir');\nassert(is_string($extraArgs) || is_null($extraArgs));\nassert(is_string($workingDir));\n<\/code><\/pre>\n\n<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>\n\n<h2 id=\"here%27s-the-thing\">Here'sthething<\/h2>\n\n<p>AlthoughtheextracodegetsPHPStantopass,ititworthit?<\/p>\n\n<p>Isthisextracodeaddingvalue?Doesitmakethecodemorereadableorisitlikelytopreventabug?<\/p>\n\n<p>Inthiscase,IknowthevaluewillalwaysbethetypeIexpect.<\/p>\n\n<p>Icanworkaroundthisusingabase