Skip to content

Commit

Permalink
[2.3] Static Code Analysis for Components
Browse files Browse the repository at this point in the history
  • Loading branch information
kalessil authored and fabpot committed Apr 15, 2015
1 parent 986e3d1 commit 78cc93c
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/BrowserKit/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function __toString()
$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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ private function sortCommands(array $commands)
}
ksort($namespacedCommands);

foreach ($namespacedCommands as &$commands) {
ksort($commands);
foreach ($namespacedCommands as &$commandsSet) {
ksort($commandsSet);
}
// unset reference to keep scope clear
unset($commandsSet);

return $namespacedCommands;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/DialogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function select(OutputInterface $output, $question, $choices, $default =
if (empty($choices[$value])) {
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
}
array_push($multiselectChoices, $value);
$multiselectChoices[] = $value;
}

if ($multiselect) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/TableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function addRow(array $row)
reset($this->rows);

foreach ($row as $key => $cellValue) {
if (!strstr($cellValue, "\n")) {
if (false === strpos($cellValue, "\n")) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function process(ContainerBuilder $container)
$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);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
}

$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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private function validate($content, $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'))) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function get($name)
}

$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;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DomCrawler/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function canonicalizePath($path)
if ('..' === $segment) {
array_pop($output);
} elseif ('.' !== $segment) {
array_push($output, $segment);
$output[] = $segment;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function hasListeners($eventName = null)
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getListeners($eventName = null)
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function assertOrderedIteratorForGroups($expected, \Traversable $itera
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function transform($dateTime)
// remove leading zeros
$entry = (string) (int) $entry;
}
// unset reference to keep scope clear
unset($entry);
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function onSubmit(FormEvent $event)
}

// 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;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1546,8 +1546,8 @@ public function getLanguages()

$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ private function unwrapListener($listener, $eventId)
// 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])) {
Expand Down

1 comment on commit 78cc93c

@TomasVotruba
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Much more readable

Please sign in to comment.