Remove obsolete code

This commit is contained in:
Fabien Potencier
2024-08-15 20:59:30 +02:00
parent 4bade436a4
commit a30d07eeb4
2 changed files with 0 additions and 334 deletions
-182
View File
@@ -122,188 +122,6 @@ abstract class CallExpression extends AbstractExpression
$compiler->raw(')');
}
/**
* @deprecated since 3.12, use Twig\Util\CallableArgumentsExtractor::getArguments() instead
*/
protected function getArguments($callable, $arguments)
{
trigger_deprecation('twig/twig', '3.12', 'The "%s()" method is deprecated, use Twig\Util\CallableArgumentsExtractor::getArguments() instead.', __METHOD__);
$callType = $this->getAttribute('type');
$callName = $this->getAttribute('name');
$parameters = [];
$named = false;
foreach ($arguments as $name => $node) {
if (!\is_int($name)) {
$named = true;
$name = $this->normalizeName($name);
} elseif ($named) {
throw new SyntaxError(\sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
}
$parameters[$name] = $node;
}
$isVariadic = $this->getAttribute('twig_callable')->isVariadic();
if (!$named && !$isVariadic) {
return $parameters;
}
if (!$callable) {
if ($named) {
$message = \sprintf('Named arguments are not supported for %s "%s".', $callType, $callName);
} else {
$message = \sprintf('Arbitrary positional arguments are not supported for %s "%s".', $callType, $callName);
}
throw new \LogicException($message);
}
[$callableParameters, $isPhpVariadic] = $this->getCallableParameters($callable, $isVariadic);
$arguments = [];
$names = [];
$missingArguments = [];
$optionalArguments = [];
$pos = 0;
foreach ($callableParameters as $callableParameter) {
$name = $this->normalizeName($callableParameter->name);
if ('range' === $callable) {
if ('start' === $name) {
$name = 'low';
} elseif ('end' === $name) {
$name = 'high';
}
}
$names[] = $name;
if (\array_key_exists($name, $parameters)) {
if (\array_key_exists($pos, $parameters)) {
throw new SyntaxError(\sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
}
if (\count($missingArguments)) {
throw new SyntaxError(\sprintf(
'Argument "%s" could not be assigned for %s "%s(%s)" because it is mapped to an internal PHP function which cannot determine default value for optional argument%s "%s".',
$name, $callType, $callName, implode(', ', $names), \count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments)
), $this->getTemplateLine(), $this->getSourceContext());
}
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$name];
unset($parameters[$name]);
$optionalArguments = [];
} elseif (\array_key_exists($pos, $parameters)) {
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$pos];
unset($parameters[$pos]);
$optionalArguments = [];
++$pos;
} elseif ($callableParameter->isDefaultValueAvailable()) {
$optionalArguments[] = new ConstantExpression($callableParameter->getDefaultValue(), -1);
} elseif ($callableParameter->isOptional()) {
if (empty($parameters)) {
break;
} else {
$missingArguments[] = $name;
}
} else {
throw new SyntaxError(\sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
}
}
if ($isVariadic) {
$arbitraryArguments = $isPhpVariadic ? new VariadicExpression([], -1) : new ArrayExpression([], -1);
foreach ($parameters as $key => $value) {
if (\is_int($key)) {
$arbitraryArguments->addElement($value);
} else {
$arbitraryArguments->addElement($value, new ConstantExpression($key, -1));
}
unset($parameters[$key]);
}
if ($arbitraryArguments->count()) {
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $arbitraryArguments;
}
}
if (!empty($parameters)) {
$unknownParameter = null;
foreach ($parameters as $parameter) {
if ($parameter instanceof Node) {
$unknownParameter = $parameter;
break;
}
}
throw new SyntaxError(
\sprintf(
'Unknown argument%s "%s" for %s "%s(%s)".',
\count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names)
),
$unknownParameter ? $unknownParameter->getTemplateLine() : $this->getTemplateLine(),
$unknownParameter ? $unknownParameter->getSourceContext() : $this->getSourceContext()
);
}
return $arguments;
}
/**
* @deprecated since 3.12
*/
protected function normalizeName(string $name): string
{
trigger_deprecation('twig/twig', '3.12', 'The "%s()" method is deprecated.', __METHOD__);
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name));
}
// To be removed in 4.0
private function getCallableParameters($callable, bool $isVariadic): array
{
$twigCallable = $this->getAttribute('twig_callable');
$rc = $this->reflectCallable($callable);
$r = $rc->getReflector();
$callableName = $rc->getName();
$parameters = $r->getParameters();
if ($this->hasNode('node')) {
array_shift($parameters);
}
if ($twigCallable->needsCharset()) {
array_shift($parameters);
}
if ($twigCallable->needsEnvironment()) {
array_shift($parameters);
}
if ($twigCallable->needsContext()) {
array_shift($parameters);
}
foreach ($twigCallable->getArguments() as $argument) {
array_shift($parameters);
}
$isPhpVariadic = false;
if ($isVariadic) {
$argument = end($parameters);
$isArray = $argument && $argument->hasType() && $argument->getType() instanceof \ReflectionNamedType && 'array' === $argument->getType()->getName();
if ($isArray && $argument->isDefaultValueAvailable() && [] === $argument->getDefaultValue()) {
array_pop($parameters);
} elseif ($argument && $argument->isVariadic()) {
array_pop($parameters);
$isPhpVariadic = true;
} else {
throw new \LogicException(\sprintf('The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = []".', $callableName, $this->getAttribute('type'), $twigCallable->getName()));
}
}
return [$parameters, $isPhpVariadic];
}
private function reflectCallable($callable): ReflectionCallable
{
return $this->reflector ??= new ReflectionCallable($callable, $this->getAttribute('type'), $this->getAttribute('name'));
-152
View File
@@ -1,152 +0,0 @@
<?php
namespace Twig\Tests\Node\Expression;
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\FunctionExpression;
use Twig\Node\Node;
use Twig\TwigFunction;
/**
* @group legacy
*/
class CallTest extends TestCase
{
public function testGetArguments()
{
$node = $this->createFunctionExpression('date');
$this->assertEquals(['U', null], $this->getArguments($node, ['date', ['format' => 'U', 'timestamp' => null]]));
}
public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments()
{
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Positional arguments cannot be used after named arguments for function "date".');
$node = $this->createFunctionExpression('date');
$this->getArguments($node, ['date', ['timestamp' => 123456, 'Y-m-d']]);
}
public function testGetArgumentsWhenArgumentIsDefinedTwice()
{
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Argument "format" is defined twice for function "date".');
$node = $this->createFunctionExpression('date');
$this->getArguments($node, ['date', ['Y-m-d', 'format' => 'U']]);
}
public function testGetArgumentsWithWrongNamedArgumentName()
{
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Unknown argument "unknown" for function "date(format, timestamp)".');
$node = $this->createFunctionExpression('date');
$this->getArguments($node, ['date', ['Y-m-d', 'timestamp' => null, 'unknown' => '']]);
}
public function testGetArgumentsWithWrongNamedArgumentNames()
{
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)".');
$node = $this->createFunctionExpression('date');
$this->getArguments($node, ['date', ['Y-m-d', 'timestamp' => null, 'unknown1' => '', 'unknown2' => '']]);
}
public function testResolveArgumentsOnlyNecessaryArgumentsForCustomFunction()
{
$node = $this->createFunctionExpression('custom_function');
$this->assertEquals(['arg1'], $this->getArguments($node, [[$this, 'customFunction'], ['arg1' => 'arg1']]));
}
public function testGetArgumentsForStaticMethod()
{
$node = $this->createFunctionExpression('custom_static_function');
$this->assertEquals(['arg1'], $this->getArguments($node, [__CLASS__.'::customStaticFunction', ['arg1' => 'arg1']]));
}
public function testResolveArgumentsWithMissingParameterForArbitraryArguments()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The last parameter of "Twig\\Tests\\Node\\Expression\\CallTest::customFunctionWithArbitraryArguments" for function "foo" must be an array with default value, eg. "array $arg = []".');
$node = $this->createFunctionExpression('foo', true);
$this->getArguments($node, [[$this, 'customFunctionWithArbitraryArguments'], []]);
}
public function testGetArgumentsWithInvalidCallable()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Callback for function "foo" is not callable in the current scope.');
$node = $this->createFunctionExpression('foo', true);
$this->getArguments($node, ['<not-a-callable>', []]);
}
public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnFunction()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('#^The last parameter of "Twig\\\\Tests\\\\Node\\\\Expression\\\\custom_call_test_function" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#');
$node = $this->createFunctionExpression('foo', true);
$this->getArguments($node, ['Twig\Tests\Node\Expression\custom_call_test_function', []]);
}
public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnObject()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('#^The last parameter of "Twig\\\\Tests\\\\Node\\\\Expression\\\\CallableTestClass\\:\\:__invoke" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#');
$node = $this->createFunctionExpression('foo', true);
$this->getArguments($node, [new CallableTestClass(), []]);
}
public static function customStaticFunction($arg1, $arg2 = 'default', $arg3 = [])
{
}
public function customFunction($arg1, $arg2 = 'default', $arg3 = [])
{
}
public function customFunctionWithArbitraryArguments()
{
}
private function getArguments($call, $args)
{
$m = new \ReflectionMethod($call, 'getArguments');
return $m->invokeArgs($call, $args);
}
private function createFunctionExpression($name, $isVariadic = false): Node_Expression_Call
{
return new Node_Expression_Call(new TwigFunction($name, null, ['is_variadic' => $isVariadic]), new Node([]), 0);
}
}
class Node_Expression_Call extends FunctionExpression
{
}
class CallableTestClass
{
public function __invoke($required)
{
}
}
function custom_call_test_function($required)
{
}