[2.3] Static Code Analysis for Components

This commit is contained in:
Vladimir Reznichenko 2015-04-09 16:55:17 +02:00 committed by Fabien Potencier
parent 986e3d1b70
commit 78cc93c3b2
17 changed files with 25 additions and 20 deletions

View File

@ -93,6 +93,7 @@ class Cookie
$dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT')); $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
if ($dateTime === false) { if ($dateTime === false) {
// this throw will provoke PHP fatal
throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.'), $this->expires); throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.'), $this->expires);
} }

View File

@ -144,9 +144,11 @@ class ApplicationDescription
} }
ksort($namespacedCommands); ksort($namespacedCommands);
foreach ($namespacedCommands as &$commands) { foreach ($namespacedCommands as &$commandsSet) {
ksort($commands); ksort($commandsSet);
} }
// unset reference to keep scope clear
unset($commandsSet);
return $namespacedCommands; return $namespacedCommands;
} }

View File

@ -71,7 +71,7 @@ class DialogHelper extends Helper
if (empty($choices[$value])) { if (empty($choices[$value])) {
throw new \InvalidArgumentException(sprintf($errorMessage, $value)); throw new \InvalidArgumentException(sprintf($errorMessage, $value));
} }
array_push($multiselectChoices, $value); $multiselectChoices[] = $value;
} }
if ($multiselect) { if ($multiselect) {

View File

@ -148,7 +148,7 @@ class TableHelper extends Helper
reset($this->rows); reset($this->rows);
foreach ($row as $key => $cellValue) { foreach ($row as $key => $cellValue) {
if (!strstr($cellValue, "\n")) { if (false === strpos($cellValue, "\n")) {
continue; continue;
} }

View File

@ -39,7 +39,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
$this->compiler = $container->getCompiler(); $this->compiler = $container->getCompiler();
$this->formatter = $this->compiler->getLoggingFormatter(); $this->formatter = $this->compiler->getLoggingFormatter();
foreach (array_keys($container->getDefinitions()) as $id) { foreach ($container->getDefinitions() as $id => $definition) {
// yes, we are specifically fetching the definition from the // yes, we are specifically fetching the definition from the
// container to ensure we are not operating on stale data // container to ensure we are not operating on stale data
$definition = $container->getDefinition($id); $definition = $container->getDefinition($id);

View File

@ -312,7 +312,7 @@ class Container implements IntrospectableContainerInterface
} }
$alternatives = array(); $alternatives = array();
foreach (array_keys($this->services) as $key) { foreach ($this->services as $key => $associatedService) {
$lev = levenshtein($id, $key); $lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) { if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$alternatives[] = $key; $alternatives[] = $key;

View File

@ -304,7 +304,7 @@ class YamlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)); throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
} }
foreach (array_keys($content) as $namespace) { foreach ($content as $namespace => $data) {
if (in_array($namespace, array('imports', 'parameters', 'services'))) { if (in_array($namespace, array('imports', 'parameters', 'services'))) {
continue; continue;
} }

View File

@ -98,7 +98,7 @@ class ParameterBag implements ParameterBagInterface
} }
$alternatives = array(); $alternatives = array();
foreach (array_keys($this->parameters) as $key) { foreach ($this->parameters as $key => $parameterValue) {
$lev = levenshtein($name, $key); $lev = levenshtein($name, $key);
if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) { if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) {
$alternatives[] = $key; $alternatives[] = $key;

View File

@ -163,7 +163,7 @@ class Link
if ('..' === $segment) { if ('..' === $segment) {
array_pop($output); array_pop($output);
} elseif ('.' !== $segment) { } elseif ('.' !== $segment) {
array_push($output, $segment); $output[] = $segment;
} }
} }

View File

@ -124,7 +124,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
public function getListeners($eventName = null) public function getListeners($eventName = null)
{ {
if (null === $eventName) { if (null === $eventName) {
foreach (array_keys($this->listenerIds) as $serviceEventName) { foreach ($this->listenerIds as $serviceEventName => $args) {
$this->lazyLoad($serviceEventName); $this->lazyLoad($serviceEventName);
} }
} else { } else {

View File

@ -68,7 +68,7 @@ class EventDispatcher implements EventDispatcherInterface
return $this->sorted[$eventName]; return $this->sorted[$eventName];
} }
foreach (array_keys($this->listeners) as $eventName) { foreach ($this->listeners as $eventName => $eventListeners) {
if (!isset($this->sorted[$eventName])) { if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName); $this->sortListeners($eventName);
} }

View File

@ -51,7 +51,7 @@ abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
foreach ($expected as $subarray) { foreach ($expected as $subarray) {
$temp = array(); $temp = array();
while (count($values) && count($temp) < count($subarray)) { while (count($values) && count($temp) < count($subarray)) {
array_push($temp, array_shift($values)); $temp[] = array_shift($values);
} }
sort($temp); sort($temp);
sort($subarray); sort($subarray);

View File

@ -40,11 +40,11 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface
*/ */
public function __construct($inputTimezone = null, $outputTimezone = null) public function __construct($inputTimezone = null, $outputTimezone = null)
{ {
if (!is_string($inputTimezone) && null !== $inputTimezone) { if (null !== $inputTimezone && !is_string($inputTimezone)) {
throw new UnexpectedTypeException($inputTimezone, 'string'); throw new UnexpectedTypeException($inputTimezone, 'string');
} }
if (!is_string($outputTimezone) && null !== $outputTimezone) { if (null !== $outputTimezone && !is_string($outputTimezone)) {
throw new UnexpectedTypeException($outputTimezone, 'string'); throw new UnexpectedTypeException($outputTimezone, 'string');
} }

View File

@ -99,6 +99,8 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
// remove leading zeros // remove leading zeros
$entry = (string) (int) $entry; $entry = (string) (int) $entry;
} }
// unset reference to keep scope clear
unset($entry);
} }
return $result; return $result;

View File

@ -74,7 +74,7 @@ class MergeCollectionListener implements EventSubscriberInterface
} }
// If we are not allowed to change anything, return immediately // If we are not allowed to change anything, return immediately
if ((!$this->allowAdd && !$this->allowDelete) || $data === $dataToMergeInto) { if ($data === $dataToMergeInto || (!$this->allowAdd && !$this->allowDelete)) {
$event->setData($dataToMergeInto); $event->setData($dataToMergeInto);
return; return;

View File

@ -1546,8 +1546,8 @@ class Request
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all(); $languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
$this->languages = array(); $this->languages = array();
foreach (array_keys($languages) as $lang) { foreach ($languages as $lang => $acceptHeaderItem) {
if (strstr($lang, '-')) { if (false !== strpos($lang, '-')) {
$codes = explode('-', $lang); $codes = explode('-', $lang);
if ('i' === $codes[0]) { if ('i' === $codes[0]) {
// Language not listed in ISO 639 that are not variants // Language not listed in ISO 639 that are not variants

View File

@ -472,9 +472,9 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
// get the original listener // get the original listener
if (is_object($listener)) { if (is_object($listener)) {
if (null === $eventId) { if (null === $eventId) {
foreach (array_keys($this->wrappedListeners) as $eventId) { foreach ($this->wrappedListeners as $eventId => $eventListeners) {
if (isset($this->wrappedListeners[$eventId][$listener])) { if (isset($eventListeners[$listener])) {
return $this->wrappedListeners[$eventId][$listener]; return $eventListeners[$listener];
} }
} }
} elseif (isset($this->wrappedListeners[$eventId][$listener])) { } elseif (isset($this->wrappedListeners[$eventId][$listener])) {