[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'));
if ($dateTime === false) {
// this throw will provoke PHP fatal
throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.'), $this->expires);
}

View File

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

View File

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

View File

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

View File

@ -39,7 +39,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
$this->compiler = $container->getCompiler();
$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
// container to ensure we are not operating on stale data
$definition = $container->getDefinition($id);

View File

@ -312,7 +312,7 @@ class Container implements IntrospectableContainerInterface
}
$alternatives = array();
foreach (array_keys($this->services) as $key) {
foreach ($this->services as $key => $associatedService) {
$lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$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));
}
foreach (array_keys($content) as $namespace) {
foreach ($content as $namespace => $data) {
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
continue;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -74,7 +74,7 @@ class MergeCollectionListener implements EventSubscriberInterface
}
// 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);
return;

View File

@ -1546,8 +1546,8 @@ class Request
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
$this->languages = array();
foreach (array_keys($languages) as $lang) {
if (strstr($lang, '-')) {
foreach ($languages as $lang => $acceptHeaderItem) {
if (false !== strpos($lang, '-')) {
$codes = explode('-', $lang);
if ('i' === $codes[0]) {
// 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
if (is_object($listener)) {
if (null === $eventId) {
foreach (array_keys($this->wrappedListeners) as $eventId) {
if (isset($this->wrappedListeners[$eventId][$listener])) {
return $this->wrappedListeners[$eventId][$listener];
foreach ($this->wrappedListeners as $eventId => $eventListeners) {
if (isset($eventListeners[$listener])) {
return $eventListeners[$listener];
}
}
} elseif (isset($this->wrappedListeners[$eventId][$listener])) {