Versa<\/a>, I was experimenting with different PHPStan levels.<\/p>\n\nLevel 1 is the least strict level, and applies the fewest rules and returns the fewest results.<\/p>\n\n
As you increase the level, the stricter PHPStan is.<\/p>\n\n
Levelling up to 9<\/h2>\n\n
Here is the code to get the values of the --extra-args<\/code> and --working-dir<\/code> options:<\/p>\n\n$extraArgs = $input->getOption('extra-args');\n$workingDir = $input->getOption('working-dir');\n<\/code><\/pre>\n\nThis passed PHPStan level 8, but these are the errors I get when running level 9:<\/p>\n\n
------ -------------------------------------------------------------------------------------------------------\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\nThe issue is that $input->getOption()<\/code> from Symfony's InputInterface<\/code> returns mixed<\/code> - even though it always returns null<\/code> or a string.<\/p>\n\nIf I did --working-dir 2<\/code>, it would return the string \"2\"<\/code>.<\/p>\n\nAn empty string throws an error, and an empty value (if there are no extra arguments) returns null<\/code>.<\/p>\n\nSo, I know $workingDir<\/code> will always be a string and $extraArgs<\/code> will either a string or null<\/code>.<\/p>\n\nPassing level 8<\/h2>\n\n
To pass level 8, PHPStan needs to be sure the variables are what I think they are.<\/p>\n\n
Here's the code I can use to get it to pass:<\/p>\n\n
$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\nBy using 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\nHere's the thing<\/h2>\n\n
Although the extra code gets PHPStan to pass, it it worth it?<\/p>\n\n
Is this extra code adding value? Does it make the code more readable or is it likely to prevent a bug?<\/p>\n\n
In this case, I know the value will always be the type I expect.<\/p>\n\n
I can work around this using a baseline or annotations for PHPStan to ignore these lines, or is level 8 good enough for this project<\/strong>?<\/p>\n\nSimilar 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>\n\n
Which level is right for you and this codebase?<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "1970-01-01T00:33:45+00:00",
"guid": null,
"hash": "04a7f9d899ae8eb0f4c33ff32a7dabae",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}