minor #17085 [2.3] Static Code Analysis for Components (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
This commit is contained in:
Fabien Potencier 2015-12-31 19:24:03 +01:00
commit 1b15ac3bdc
9 changed files with 20 additions and 26 deletions

View File

@ -116,8 +116,8 @@ class ClassCollectionLoader
} }
// cache the core classes // cache the core classes
if (!is_dir(dirname($cache))) { if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
mkdir(dirname($cache), 0777, true); throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir));
} }
self::writeCacheFile($cache, '<?php '.$content); self::writeCacheFile($cache, '<?php '.$content);

View File

@ -134,7 +134,7 @@ class ClassMapGenerator
} }
if ($isClassConstant) { if ($isClassConstant) {
continue; break;
} }
// Find the classname // Find the classname

View File

@ -37,7 +37,7 @@ class TextDescriptor extends Descriptor
$nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($argument->getName()); $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($argument->getName());
$output = str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $argument->getDescription()); $output = str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $argument->getDescription());
$output = sprintf(" <info>%-${nameWidth}s</info> %s%s", $argument->getName(), $output, $default); $output = sprintf(" <info>%-{$nameWidth}s</info> %s%s", $argument->getName(), $output, $default);
return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output; return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
} }
@ -56,7 +56,7 @@ class TextDescriptor extends Descriptor
$nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName()); $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName());
$nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2; $nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2;
$output = sprintf(" <info>%s</info> %-${nameWithShortcutWidth}s%s%s%s", $output = sprintf(" <info>%s</info> %-{$nameWithShortcutWidth}s%s%s%s",
'--'.$option->getName(), '--'.$option->getName(),
$option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '',
str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $option->getDescription()), str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $option->getDescription()),
@ -146,7 +146,7 @@ class TextDescriptor extends Descriptor
$width = $this->getColumnWidth($description->getCommands()); $width = $this->getColumnWidth($description->getCommands());
foreach ($description->getCommands() as $command) { foreach ($description->getCommands() as $command) {
$messages[] = sprintf("%-${width}s %s", $command->getName(), $command->getDescription()); $messages[] = sprintf("%-{$width}s %s", $command->getName(), $command->getDescription());
} }
} else { } else {
$width = $this->getColumnWidth($description->getCommands()); $width = $this->getColumnWidth($description->getCommands());
@ -167,7 +167,7 @@ class TextDescriptor extends Descriptor
} }
foreach ($namespace['commands'] as $name) { foreach ($namespace['commands'] as $name) {
$messages[] = sprintf(" <info>%-${width}s</info> %s", $name, $description->getCommand($name)->getDescription()); $messages[] = sprintf(" <info>%-{$width}s</info> %s", $name, $description->getCommand($name)->getDescription());
} }
} }
} }

View File

@ -46,7 +46,7 @@ class DialogHelper extends Helper
$messages = (array) $question; $messages = (array) $question;
foreach ($choices as $key => $value) { foreach ($choices as $key => $value) {
$messages[] = sprintf(" [<info>%-${width}s</info>] %s", $key, $value); $messages[] = sprintf(" [<info>%-{$width}s</info>] %s", $key, $value);
} }
$output->writeln($messages); $output->writeln($messages);

View File

@ -48,8 +48,8 @@ class NativeFileSessionHandler extends NativeSessionHandler
$baseDir = ltrim(strrchr($savePath, ';'), ';'); $baseDir = ltrim(strrchr($savePath, ';'), ';');
} }
if ($baseDir && !is_dir($baseDir)) { if ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {
mkdir($baseDir, 0777, true); throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $baseDir));
} }
ini_set('session.save_path', $savePath); ini_set('session.save_path', $savePath);

View File

@ -42,8 +42,8 @@ class MockFileSessionStorage extends MockArraySessionStorage
$savePath = sys_get_temp_dir(); $savePath = sys_get_temp_dir();
} }
if (!is_dir($savePath)) { if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
mkdir($savePath, 0777, true); throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $savePath));
} }
$this->savePath = $savePath; $this->savePath = $savePath;

View File

@ -69,8 +69,8 @@ class CacheLoader extends Loader
$content = $storage->getContent(); $content = $storage->getContent();
if (!is_dir($dir)) { if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
mkdir($dir, 0777, true); throw new \RuntimeException(sprintf('Cache Loader was not able to create directory "%s"', $dir));
} }
file_put_contents($path, $content); file_put_contents($path, $content);

View File

@ -34,8 +34,8 @@ class IcuResFileDumper implements DumperInterface
$file = $messages->getLocale().'.'.$this->getExtension(); $file = $messages->getLocale().'.'.$this->getExtension();
$path = $options['path'].'/'.$domain.'/'; $path = $options['path'].'/'.$domain.'/';
if (!file_exists($path)) { if (!is_dir($path) && !@mkdir($path) && !is_dir($path)) {
mkdir($path); throw new \RuntimeException(sprintf('File Dumper was not able to create directory "%s"', $path));
} }
// backup // backup
@ -102,11 +102,7 @@ class IcuResFileDumper implements DumperInterface
1, 4, 0, 0 // Unicode version 1, 4, 0, 0 // Unicode version
); );
$output = $header return $header.$root.$data;
.$root
.$data;
return $output;
} }
private function writePadding($data) private function writePadding($data)
@ -120,9 +116,7 @@ class IcuResFileDumper implements DumperInterface
private function getPosition($data) private function getPosition($data)
{ {
$position = (strlen($data) + 28) / 4; return (strlen($data) + 28) / 4;
return $position;
} }
/** /**

View File

@ -67,8 +67,8 @@ class TranslationWriter
// get the right dumper // get the right dumper
$dumper = $this->dumpers[$format]; $dumper = $this->dumpers[$format];
if (isset($options['path']) && !is_dir($options['path'])) { if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
mkdir($options['path'], 0777, true); throw new \RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
} }
// save // save