Merge branch '4.4' into 5.2

* 4.4:
  add missing return type declaration
  Modernize func_get_args() calls to variadic parameters
  Use a lazyintertor to close files descriptors when no longer used
This commit is contained in:
Christian Flothmann 2021-02-12 11:38:38 +01:00
commit 6dce3227db
8 changed files with 103 additions and 14 deletions

View File

@ -84,12 +84,12 @@ class ExpressionFunction
throw new \InvalidArgumentException(sprintf('An expression function name must be defined when PHP function "%s" is namespaced.', $phpFunctionName)); throw new \InvalidArgumentException(sprintf('An expression function name must be defined when PHP function "%s" is namespaced.', $phpFunctionName));
} }
$compiler = function () use ($phpFunctionName) { $compiler = function (...$args) use ($phpFunctionName) {
return sprintf('\%s(%s)', $phpFunctionName, implode(', ', \func_get_args())); return sprintf('\%s(%s)', $phpFunctionName, implode(', ', $args));
}; };
$evaluator = function () use ($phpFunctionName) { $evaluator = function ($p, ...$args) use ($phpFunctionName) {
return $phpFunctionName(...\array_slice(\func_get_args(), 1)); return $phpFunctionName(...$args);
}; };
return new self($expressionFunctionName ?: end($parts), $compiler, $evaluator); return new self($expressionFunctionName ?: end($parts), $compiler, $evaluator);

View File

@ -717,14 +717,16 @@ class Filesystem
} }
/** /**
* @param mixed ...$args
*
* @return mixed * @return mixed
*/ */
private static function box(callable $func) private static function box(callable $func, ...$args)
{ {
self::$lastError = null; self::$lastError = null;
set_error_handler(__CLASS__.'::handleError'); set_error_handler(__CLASS__.'::handleError');
try { try {
$result = $func(...\array_slice(\func_get_args(), 1)); $result = $func(...$args);
restore_error_handler(); restore_error_handler();
return $result; return $result;

View File

@ -20,6 +20,7 @@ use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator; use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
use Symfony\Component\Finder\Iterator\FilecontentFilterIterator; use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
use Symfony\Component\Finder\Iterator\FilenameFilterIterator; use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
use Symfony\Component\Finder\Iterator\LazyIterator;
use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator; use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
use Symfony\Component\Finder\Iterator\SortableIterator; use Symfony\Component\Finder\Iterator\SortableIterator;
@ -622,7 +623,9 @@ class Finder implements \IteratorAggregate, \Countable
$iterator = new \AppendIterator(); $iterator = new \AppendIterator();
foreach ($this->dirs as $dir) { foreach ($this->dirs as $dir) {
$iterator->append($this->searchInDirectory($dir)); $iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
return $this->searchInDirectory($dir);
})));
} }
foreach ($this->iterators as $it) { foreach ($this->iterators as $it) {

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Finder\Iterator;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class LazyIterator implements \IteratorAggregate
{
private $iteratorFactory;
public function __construct(callable $iteratorFactory)
{
$this->iteratorFactory = $iteratorFactory;
}
public function getIterator(): \Traversable
{
yield from ($this->iteratorFactory)();
}
}

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Finder\Tests\Iterator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Iterator\LazyIterator;
class LazyIteratorTest extends TestCase
{
public function testLazy()
{
new LazyIterator(function () {
$this->markTestFailed('lazyIterator should not be called');
});
$this->expectNotToPerformAssertions();
}
public function testDelegate()
{
$iterator = new LazyIterator(function () {
return new Iterator(['foo', 'bar']);
});
$this->assertCount(2, $iterator);
}
public function testInnerDestructedAtTheEnd()
{
$count = 0;
$iterator = new LazyIterator(function () use (&$count) {
++$count;
return new Iterator(['foo', 'bar']);
});
foreach ($iterator as $x) {
}
$this->assertSame(1, $count);
foreach ($iterator as $x) {
}
$this->assertSame(2, $count);
}
}

View File

@ -27,5 +27,10 @@ abstract class AbstractLocaleTest extends TestCase
$this->assertSame('en_GB', $this->call('getDefault')); $this->assertSame('en_GB', $this->call('getDefault'));
} }
abstract protected function call($methodName); /**
* @param mixed ...$args
*
* @return mixed
*/
abstract protected function call(string $methodName, ...$args);
} }

View File

@ -139,10 +139,8 @@ class LocaleTest extends AbstractLocaleTest
$this->assertSame('en', $this->call('getDefault')); $this->assertSame('en', $this->call('getDefault'));
} }
protected function call($methodName) protected function call(string $methodName, ...$args)
{ {
$args = \array_slice(\func_get_args(), 1);
return Locale::{$methodName}(...$args); return Locale::{$methodName}(...$args);
} }
} }

View File

@ -29,10 +29,8 @@ class LocaleTest extends AbstractLocaleTest
parent::setUp(); parent::setUp();
} }
protected function call($methodName) protected function call(string $methodName, ...$args)
{ {
$args = \array_slice(\func_get_args(), 1);
return \Locale::{$methodName}(...$args); return \Locale::{$methodName}(...$args);
} }
} }