Fix extract method to avoid recalculating count() for each iteration.

Cache the value of `count($attributes)` to avoid recalculating it for each iteration of the loop.

Since this count will never change during the method execution, this change make the code a bit more efficient.
This commit is contained in:
Youpie 2013-11-29 01:05:12 +01:00 committed by Fabien Potencier
parent a95503abc8
commit 3447f896a7

View File

@ -504,6 +504,7 @@ class Crawler extends \SplObjectStorage
public function extract($attributes)
{
$attributes = (array) $attributes;
$count = count($attributes);
$data = array();
foreach ($this as $node) {
@ -516,7 +517,7 @@ class Crawler extends \SplObjectStorage
}
}
$data[] = count($attributes) > 1 ? $elements : $elements[0];
$data[] = $count > 1 ? $elements : $elements[0];
}
return $data;