Skip to content

[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

Closed
wants to merge 9 commits into from
Closed

[2.3] Static Code Analysis for Components #17085

wants to merge 9 commits into from

Conversation

kalessil
Copy link
Contributor

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

@EVODelavega
Copy link
Contributor

Please use sprintf for exception messages, instead of concatenating variables into the message:

Yes, using sprintf is what the coding standard should document. Can you
submit a PR on the docs? And perhaps a PR on symfony/symfony to fix
inlined exceptions?

Thanks,
Fabien

Also see Symfony coding standards last item under structure:

Exception message strings should be concatenated using sprintf.

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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

@Tobion
Copy link
Contributor

Tobion commented Dec 21, 2015

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.

@kalessil
Copy link
Contributor Author

@Tobion : the same as in #16292 - pattern and solution; I'm not sure if this breaks BC.

@kalessil
Copy link
Contributor Author

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));
Copy link
Member

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...

Copy link
Contributor Author

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"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

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);
Copy link
Member

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)

Copy link
Contributor Author

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 ${...} structure. Should I make adjustments to {$...} then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go for me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${...} -> {$...}: done

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));
Copy link
Member

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)

Copy link
Contributor Author

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

@nicolas-grekas
Copy link
Member

👍
Status: reviewed

@@ -134,7 +134,7 @@ private static function findClasses($path)
}

if ($isClassConstant) {
continue;
break;
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh god PHP :(

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@xabbuh
Copy link
Member

xabbuh commented Dec 31, 2015

👍

@fabpot
Copy link
Member

fabpot commented Dec 31, 2015

Thank you @kalessil.

fabpot added a commit that referenced this pull request Dec 31, 2015
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
@fabpot fabpot closed this Dec 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants