minor #13995 [2.3] Static Code Analysis for Components (kalessil)

This PR was squashed before being merged into the 2.3 branch (closes #13995).

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), no functional changes:
- [Filesystem] : nested ifs, not optimal ifs
- in_array() miss-uses
- class re-implements an interface of super(s)
- array_keys/array_values as foreach array - fixed cases where changes has sense

Commits
-------

4abfabf [2.3] Static Code Analysis for Components
This commit is contained in:
Fabien Potencier 2015-03-21 18:48:07 +01:00
commit 32964969d7
15 changed files with 28 additions and 31 deletions

View File

@ -19,6 +19,6 @@ namespace Symfony\Component\CssSelector\Exception;
* *
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/ */
class ExpressionErrorException extends ParseException implements ExceptionInterface class ExpressionErrorException extends ParseException
{ {
} }

View File

@ -19,6 +19,6 @@ namespace Symfony\Component\CssSelector\Exception;
* *
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/ */
class InternalErrorException extends ParseException implements ExceptionInterface class InternalErrorException extends ParseException
{ {
} }

View File

@ -21,7 +21,7 @@ use Symfony\Component\CssSelector\Parser\Token;
* *
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/ */
class SyntaxErrorException extends ParseException implements ExceptionInterface class SyntaxErrorException extends ParseException
{ {
/** /**
* @param string $expectedValue * @param string $expectedValue

View File

@ -173,7 +173,7 @@ class GraphvizDumper extends Dumper
foreach ($container->getServiceIds() as $id) { foreach ($container->getServiceIds() as $id) {
$service = $container->get($id); $service = $container->get($id);
if (in_array($id, array_keys($container->getAliases()))) { if (array_key_exists($id, $container->getAliases())) {
continue; continue;
} }

View File

@ -41,10 +41,9 @@ class Filesystem
$this->mkdir(dirname($targetFile)); $this->mkdir(dirname($targetFile));
if (!$override && is_file($targetFile) && null === parse_url($originFile, PHP_URL_HOST)) { $doCopy = true;
if (!$override && null === parse_url($originFile, PHP_URL_HOST) && is_file($targetFile)) {
$doCopy = filemtime($originFile) > filemtime($targetFile); $doCopy = filemtime($originFile) > filemtime($targetFile);
} else {
$doCopy = true;
} }
if ($doCopy) { if ($doCopy) {
@ -274,7 +273,7 @@ class Filesystem
*/ */
public function symlink($originDir, $targetDir, $copyOnWindows = false) public function symlink($originDir, $targetDir, $copyOnWindows = false)
{ {
if (!function_exists('symlink') && $copyOnWindows) { if ($copyOnWindows && !function_exists('symlink')) {
$this->mirror($originDir, $targetDir); $this->mirror($originDir, $targetDir);
return; return;
@ -291,16 +290,14 @@ class Filesystem
} }
} }
if (!$ok) { if (!$ok && true !== @symlink($originDir, $targetDir)) {
if (true !== @symlink($originDir, $targetDir)) { $report = error_get_last();
$report = error_get_last(); if (is_array($report)) {
if (is_array($report)) { if ('\\' === DIRECTORY_SEPARATOR && false !== strpos($report['message'], 'error code(1314)')) {
if ('\\' === DIRECTORY_SEPARATOR && false !== strpos($report['message'], 'error code(1314)')) { throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?');
throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?');
}
} }
throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir));
} }
throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir));
} }
} }
@ -339,9 +336,9 @@ class Filesystem
$endPathRemainder = implode('/', array_slice($endPathArr, $index)); $endPathRemainder = implode('/', array_slice($endPathArr, $index));
// Construct $endPath from traversing to the common path, then to the remaining $endPath // Construct $endPath from traversing to the common path, then to the remaining $endPath
$relativePath = $traverser.(strlen($endPathRemainder) > 0 ? $endPathRemainder.'/' : ''); $relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
return (strlen($relativePath) === 0) ? './' : $relativePath; return '' === $relativePath ? './' : $relativePath;
} }
/** /**

View File

@ -184,10 +184,10 @@ class NativeRequestHandler implements RequestHandlerInterface
unset($files[$k]); unset($files[$k]);
} }
foreach (array_keys($data['name']) as $key) { foreach ($data['name'] as $key => $name) {
$files[$key] = self::fixPhpFilesArray(array( $files[$key] = self::fixPhpFilesArray(array(
'error' => $data['error'][$key], 'error' => $data['error'][$key],
'name' => $data['name'][$key], 'name' => $name,
'type' => $data['type'][$key], 'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key], 'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key], 'size' => $data['size'][$key],

View File

@ -140,10 +140,10 @@ class FileBag extends ParameterBag
unset($files[$k]); unset($files[$k]);
} }
foreach (array_keys($data['name']) as $key) { foreach ($data['name'] as $key => $name) {
$files[$key] = $this->fixPhpFilesArray(array( $files[$key] = $this->fixPhpFilesArray(array(
'error' => $data['error'][$key], 'error' => $data['error'][$key],
'name' => $data['name'][$key], 'name' => $name,
'type' => $data['type'][$key], 'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key], 'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key], 'size' => $data['size'][$key],

View File

@ -1549,7 +1549,7 @@ class Request
foreach (array_keys($languages) as $lang) { foreach (array_keys($languages) as $lang) {
if (strstr($lang, '-')) { if (strstr($lang, '-')) {
$codes = explode('-', $lang); $codes = explode('-', $lang);
if ($codes[0] == 'i') { if ('i' === $codes[0]) {
// Language not listed in ISO 639 that are not variants // Language not listed in ISO 639 that are not variants
// of any listed language, which can be registered with the // of any listed language, which can be registered with the
// i-prefix, such as i-cherokee // i-prefix, such as i-cherokee
@ -1558,7 +1558,7 @@ class Request
} }
} else { } else {
for ($i = 0, $max = count($codes); $i < $max; $i++) { for ($i = 0, $max = count($codes); $i < $max; $i++) {
if ($i == 0) { if ($i === 0) {
$lang = strtolower($codes[0]); $lang = strtolower($codes[0]);
} else { } else {
$lang .= '_'.strtoupper($codes[$i]); $lang .= '_'.strtoupper($codes[$i]);

View File

@ -75,7 +75,7 @@ class MimeTypeTest extends \PHPUnit_Framework_TestCase
$this->markTestSkipped('Can not verify chmod operations on Windows'); $this->markTestSkipped('Can not verify chmod operations on Windows');
} }
if (in_array(get_current_user(), array('root'))) { if ('root' === get_current_user()) {
$this->markTestSkipped('This test will fail if run under superuser'); $this->markTestSkipped('This test will fail if run under superuser');
} }

View File

@ -22,7 +22,7 @@ use Symfony\Component\Config\Resource\FileResource;
* *
* @api * @api
*/ */
class CsvFileLoader extends ArrayLoader implements LoaderInterface class CsvFileLoader extends ArrayLoader
{ {
private $delimiter = ';'; private $delimiter = ';';
private $enclosure = '"'; private $enclosure = '"';

View File

@ -20,7 +20,7 @@ use Symfony\Component\Config\Resource\FileResource;
* *
* @author stealth35 * @author stealth35
*/ */
class IniFileLoader extends ArrayLoader implements LoaderInterface class IniFileLoader extends ArrayLoader
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -18,7 +18,7 @@ use Symfony\Component\Config\Resource\FileResource;
/** /**
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
*/ */
class MoFileLoader extends ArrayLoader implements LoaderInterface class MoFileLoader extends ArrayLoader
{ {
/** /**
* Magic used for validating the format of a MO file as well as * Magic used for validating the format of a MO file as well as

View File

@ -22,7 +22,7 @@ use Symfony\Component\Config\Resource\FileResource;
* *
* @api * @api
*/ */
class PhpFileLoader extends ArrayLoader implements LoaderInterface class PhpFileLoader extends ArrayLoader
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -19,7 +19,7 @@ use Symfony\Component\Config\Resource\FileResource;
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
* @copyright Copyright (c) 2012, Clemens Tolboom * @copyright Copyright (c) 2012, Clemens Tolboom
*/ */
class PoFileLoader extends ArrayLoader implements LoaderInterface class PoFileLoader extends ArrayLoader
{ {
public function load($resource, $locale, $domain = 'messages') public function load($resource, $locale, $domain = 'messages')
{ {

View File

@ -24,7 +24,7 @@ use Symfony\Component\Yaml\Exception\ParseException;
* *
* @api * @api
*/ */
class YamlFileLoader extends ArrayLoader implements LoaderInterface class YamlFileLoader extends ArrayLoader
{ {
private $yamlParser; private $yamlParser;