10000 [2.3] Static Code Analysis for Components by kalessil · Pull Request #17085 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension 10000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Php Inspections (EA Extended): use sprintf for exception messages, us…
…e is_dir instead of file_exists
  • Loading branch information
kalessil committed Dec 21, 2015
commit eaada24d0247b157cfceed0b92b9d3794caf08a9
4 changes: 2 additions & 2 deletions src/Symfony/Component/ClassLoader/ClassCollectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
}

// cache the core classes
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);
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.

}
self::writeCacheFile($cache, '<?php '.$content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function __construct($savePath = null)
$baseDir = ltrim(strrchr($savePath, ';'), ';');
}

if ($baseDir && !file_exists($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {
throw new \RuntimeException('Session Storage was not able to create a folder: '.$baseDir);
if ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {
throw new \RuntimeException(sprintf('The Session Storage was not able to create a directory: %s', $baseDir));
Copy link
Member

Choose a reason for hiding this comment

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

did anyone report the need for these exceptions? I'm sure something already fails when the dir can't be created. Did you experience any "unclear" error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, it was no reports about this. Yep, had to support couple cases when settings were misconfigured and these exceptions pointing problematic configurations really fast.

In 99.9% of cases (I guess) exceptions will not make any harm due to default settings.

Copy link
Member

Choose a reason for hiding this comment

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

Ok then , thanks for the argument :)
I suggest rewording to remove the : in the message:
The Session Storage was not able to create directory "%s".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Glad to share =)

Will apply wording adjustments for all exceptions.

}

ini_set('session.save_path', $savePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function __construct($savePath = null, $name = 'MOCKSESSID', MetadataBag
$savePath = sys_get_temp_dir();
}

if (!file_exists($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
throw new \RuntimeException('Session Storage mock was not able to create a folder: '.$savePath);
if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
throw new \RuntimeException(sprintf('The Session Storage was not able to create a directory: %s', $savePath));
}

$this->savePath = $savePath;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Templating/Loader/CacheLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function load(TemplateReferenceInterface $template)

$content = $storage->getContent();

if (!file_exists($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException('Cache Loader was not able to create a folder: '.$dir);
if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Cache Loader was not able to create a directory: %s', $dir));
Copy link
Member

Choose a reason for hiding this comment

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

sometimes you have The in front, sometimes no. My soft preference would be without The, and my strong preference would be for consistency :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, will adjust.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted

}

file_put_contents($path, $content);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function dump(MessageCatalogue $messages, $options = array())
$file = $messages->getLocale().'.'.$this->getExtension();
$path = $options['path'].'/'.$domain.'/';

if (!file_exists($path) && !@mkdir($path) && !is_dir($path)) {
throw new \RuntimeException('The file dumper was not able to create a folder: '.$path);
if (!is_dir($path) && !@mkdir($path) && !is_dir($path)) {
throw new \RuntimeException(sprintf('The File Dumper was not able to create a directory: %s', $path));
}

// backup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function writeTranslations(MessageCatalogue $catalogue, $format, $options
// get the right dumper
$dumper = $this->dumpers[$format];

if (isset($options['path']) && !file_exists($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
throw new \RuntimeException('Translation Writer was not able to create a folder: '.$options['path']);
if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
throw new \RuntimeException(sprintf('Translation Writer was not able to create a directory: %s', $options['path']));
}

// save
Expand Down
0