Skip to content

Commit

Permalink
minor #3885 Use PHP 7.1 features : is_iterable, ?? and ?: (Grom…
Browse files Browse the repository at this point in the history
…NaN)

This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Use PHP 7.1 features : `is_iterable`, `??` and `?:`

Apply the following changes with rector, compatible with PHP 7.1:

```php
return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src',
        __DIR__ . '/doc',
        __DIR__ . '/tests',
        __DIR__ . '/extra',
    ]);
    $rectorConfig->rule(\Rector\Php53\Rector\Ternary\TernaryToElvisRector::class);
    $rectorConfig->rule(\Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector::class);
    $rectorConfig->rule(\Rector\Php71\Rector\BooleanOr\IsIterableRector::class);
};
```

Commits
-------

991f518 Replace calls to twig_test_iterable to is_iterable
6ca0d77 Convert Ternary to Elvis or Null Coalescing
cb8a824 Use is_iterable when possible
  • Loading branch information
fabpot committed Oct 20, 2023
2 parents 78e5245 + 991f518 commit 8b55ad4
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 3.7.2 (2023-XX-XX)
# 3.8.0 (2023-XX-XX)

* n/a
* Deprecate `twig_test_iterable` function. Use the native `is_iterable` instead.

# 3.7.1 (2023-08-28)

Expand Down
22 changes: 12 additions & 10 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function getTests(): array
new TwigTest('divisible by', null, ['node_class' => DivisiblebyTest::class, 'one_mandatory_argument' => true]),
new TwigTest('constant', null, ['node_class' => ConstantTest::class]),
new TwigTest('empty', 'twig_test_empty'),
new TwigTest('iterable', 'twig_test_iterable'),
new TwigTest('iterable', 'is_iterable'),
];
}

Expand Down Expand Up @@ -391,7 +391,7 @@ function twig_random(Environment $env, $values = null, $max = null)
}
}

if (!twig_test_iterable($values)) {
if (!is_iterable($values)) {
return $values;
}

Expand Down Expand Up @@ -528,7 +528,7 @@ function twig_date_converter(Environment $env, $date = null, $timezone = null)
*/
function twig_replace_filter($str, $from)
{
if (!twig_test_iterable($from)) {
if (!is_iterable($from)) {
throw new RuntimeError(sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".', \is_object($from) ? \get_class($from) : \gettype($from)));
}

Expand Down Expand Up @@ -625,7 +625,7 @@ function twig_array_merge(...$arrays)
$result = [];

foreach ($arrays as $argNumber => $array) {
if (!twig_test_iterable($array)) {
if (!is_iterable($array)) {
throw new RuntimeError(sprintf('The merge filter only works with arrays or "Traversable", got "%s" for argument %d.', \gettype($array), $argNumber + 1));
}

Expand Down Expand Up @@ -655,7 +655,7 @@ function twig_slice(Environment $env, $item, $start, $length = null, $preserveKe

if ($start >= 0 && $length >= 0 && $item instanceof \Iterator) {
try {
return iterator_to_array(new \LimitIterator($item, $start, null === $length ? -1 : $length), $preserveKeys);
return iterator_to_array(new \LimitIterator($item, $start, $length ?? -1), $preserveKeys);
} catch (\OutOfBoundsException $e) {
return [];
}
Expand Down Expand Up @@ -721,7 +721,7 @@ function twig_last(Environment $env, $item)
*/
function twig_join_filter($value, $glue = '', $and = null)
{
if (!twig_test_iterable($value)) {
if (!is_iterable($value)) {
$value = (array) $value;
}

Expand Down Expand Up @@ -1229,7 +1229,7 @@ function twig_call_macro(Template $template, string $method, array $args, int $l
*/
function twig_ensure_traversable($seq)
{
if ($seq instanceof \Traversable || \is_array($seq)) {
if (is_iterable($seq)) {
return $seq;
}

Expand Down Expand Up @@ -1292,10 +1292,12 @@ function twig_test_empty($value)
* @param mixed $value A variable
*
* @return bool true if the value is traversable
*
* @deprecated since Twig 3.8, to be removed in 4.0 (use the native "is_iterable" function instead)
*/
function twig_test_iterable($value)
{
return $value instanceof \Traversable || \is_array($value);
return is_iterable($value);
}

/**
Expand Down Expand Up @@ -1427,7 +1429,7 @@ function twig_constant_is_defined($constant, $object = null)
*/
function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
{
if (!twig_test_iterable($items)) {
if (!is_iterable($items)) {
throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', \is_object($items) ? \get_class($items) : \gettype($items)));
}

Expand Down Expand Up @@ -1671,7 +1673,7 @@ function twig_array_column($array, $name, $index = null): array

function twig_array_filter(Environment $env, $array, $arrow)
{
if (!twig_test_iterable($array)) {
if (!is_iterable($array)) {
throw new RuntimeError(sprintf('The "filter" filter expects an array or "Traversable", got "%s".', \is_object($array) ? \get_class($array) : \gettype($array)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FilesystemLoader implements LoaderInterface
*/
public function __construct($paths = [], string $rootPath = null)
{
$this->rootPath = (null === $rootPath ? getcwd() : $rootPath).\DIRECTORY_SEPARATOR;
$this->rootPath = ($rootPath ?? getcwd()).\DIRECTORY_SEPARATOR;
if (null !== $rootPath && false !== ($realPath = realpath($rootPath))) {
$this->rootPath = $realPath.\DIRECTORY_SEPARATOR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Node/WithNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function compile(Compiler $compiler): void
->write(sprintf('$%s = ', $varsName))
->subcompile($node)
->raw(";\n")
->write(sprintf("if (!twig_test_iterable(\$%s)) {\n", $varsName))
->write(sprintf("if (!is_iterable(\$%s)) {\n", $varsName))
->indent()
->write("throw new RuntimeError('Variables passed to the \"with\" tag must be a hash.', ")
->repr($node->getTemplateLine())
Expand Down
2 changes: 1 addition & 1 deletion src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private function needEscaping()
return $this->statusStack[\count($this->statusStack) - 1];
}

return $this->defaultStrategy ? $this->defaultStrategy : false;
return $this->defaultStrategy ?: false;
}

private function getEscaperFilter(string $type, Node $node): FilterExpression
Expand Down
2 changes: 1 addition & 1 deletion src/Profiler/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function formatTemplate(Profile $profile, $prefix): string

protected function formatNonTemplate(Profile $profile, $prefix): string
{
return sprintf('%s└ %s::%s(<span style="background-color: %s">%s</span>)', $prefix, $profile->getTemplate(), $profile->getType(), isset(self::$colors[$profile->getType()]) ? self::$colors[$profile->getType()] : 'auto', $profile->getName());
return sprintf('%s└ %s::%s(<span style="background-color: %s">%s</span>)', $prefix, $profile->getTemplate(), $profile->getType(), self::$colors[$profile->getType()] ?? 'auto', $profile->getName());
}

protected function formatTime(Profile $profile, $percent): string
Expand Down
2 changes: 1 addition & 1 deletion src/Test/NodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function assertNodeCompilation($source, Node $node, Environment $environm

protected function getCompiler(Environment $environment = null)
{
return new Compiler(null === $environment ? $this->getEnvironment() : $environment);
return new Compiler($environment ?? $this->getEnvironment());
}

protected function getEnvironment()
Expand Down

0 comments on commit 8b55ad4

Please sign in to comment.