-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.3] Static Code Analysis for Components #17085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Please use
Also see Symfony coding standards last item under structure:
|
if (!is_dir(dirname($cache))) { | ||
mkdir(dirname($cache), 0777, true); | ||
if (!file_exists($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) { | ||
throw new \RuntimeException('Class Collection Loader was not able to create a folder: '.$cacheDir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call it a 'directory' instead and make sure exceptions end with a full stop, same goes for the other exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected
According to twigphp/Twig#1944 this is not fully safe against race conditions either. And now we might throw exceptions in placed where it worked before. |
…e is_dir instead of file_exists
Exceptions message generation changed in favor of sprintf |
if (!is_dir(dirname($cache))) { | ||
mkdir(dirname($cache), 0777, true); | ||
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) { | ||
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create a directory: %s', $cacheDir)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this add anything? If the directory doesn't exists, writeCacheFile already throws an exception...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean "Failed to write cache file"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather expect the component to separate exceptions: because case "directory can not be created" and "file not dumped/file not renamed" needs different investigations.
@@ -52,7 +52,7 @@ public function __toString() | |||
foreach ($this->headers as $name => $values) { | |||
$name = implode('-', array_map('ucfirst', explode('-', $name))); | |||
foreach ($values as $value) { | |||
$content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); | |||
$content .= sprintf("%-${max}s %s\r\n", $name.':', $value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally, I prefer {$max}
; it generates much cleaner tokens when parsing (think e.g. static analyzers)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It were 4 more cases with sprintf and inline variables, as
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…consistent (${...} => {$...})
if (!is_dir(dirname($cache))) { | ||
mkdir(dirname($cache), 0777, true); | ||
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) { | ||
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create a directory "%s"', $cacheDir)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the "a" before "directory" should be remove (below also)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"a"s removed in all exceptions messages from this PR
👍 |
@@ -134,7 +134,7 @@ private static function findClasses($path) | |||
} | |||
|
|||
if ($isClassConstant) { | |||
continue; | |||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about this change? continue
relates to the for
loop while break
only leaves the switch
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, continue inside switch behaves differently in PHP than in other languages.
http://php.net/manual/en/control-structures.continue.php
Note: In PHP the switch statement is considered a looping structure for the purposes of continue. continue behaves like break (when no arguments are passed). If a switch is inside a loop, continue 2 will continue with the next iteration of the outer loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh god PHP :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully understand =)
Or take in account finally-blocks introduced in 5.5 - if you get an exception inside (even handled) you can have variety of miss-behaviors of code in different PHP versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But at least I can warn people using my plugin that something might go wrong and they can concentrate on right things instead of long debugging hours.
👍 |
Thank you @kalessil. |
This PR was squashed before being merged into the 2.3 branch (closes #17085). Discussion ---------- [2.3] Static Code Analysis for Components | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Static Code Analysis with Php Inspections (EA Extended): - remaining mkdir race conditions - continue miss-usage in switch Commits ------- 6d303c7 [2.3] Static Code Analysis for Components
Static Code Analysis with Php Inspections (EA Extended):
- remaining mkdir race conditions
- continue miss-usage in switch