Skip to content

Commit 3b70329

Browse files
committed
minor #9651 Fix extract method to avoid recalculating count() for each iteration. (fabpot)
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #9651). Discussion ---------- Fix `extract` method to avoid recalculating count() for each iteration. This PR was submitted on the symfony/DomCrawler read-only repository and moved automatically to the main Symfony repository (closes symfony/dom-crawler#16). 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. Commits ------- 9ababf4 Fix `extract` method to avoid recalculating count() for each iteration.
2 parents a95503a + 3447f89 commit 3b70329

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public function text()
504504
public function extract($attributes)
505505
{
506506
$attributes = (array) $attributes;
507+
$count = count($attributes);
507508

508509
$data = array();
509510
foreach ($this as $node) {
@@ -516,7 +517,7 @@ public function extract($attributes)
516517
}
517518
}
518519

519-
$data[] = count($attributes) > 1 ? $elements : $elements[0];
520+
$data[] = $count > 1 ? $elements : $elements[0];
520521
}
521522

522523
return $data;

0 commit comments

Comments
 (0)