Skip to content

Commit

Permalink
Merge 43d50dd into 34b94ee
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Jul 14, 2020
2 parents 34b94ee + 43d50dd commit 1f95d8f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
14 changes: 2 additions & 12 deletions phpstan.neon
Expand Up @@ -2,15 +2,5 @@ parameters:
level: 6
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
excludes_analyse:
- src/Shell/Task/TwigTemplateTask.php
ignoreErrors:
-
message: "#^Parameter \\#2 \\$callable of class Twig\\\\TwigFilter constructor expects \\(callable\\(\\)\\: mixed\\)\\|null, 'debug' given\\.$#"
count: 1
path: src/Twig/Extension/BasicExtension.php

-
message: "#^Constant CACHE not found\\.$#"
count: 1
path: src/View/TwigView.php
bootstrapFiles:
- tests/bootstrap.php
30 changes: 27 additions & 3 deletions src/Twig/Extension/BasicExtension.php
Expand Up @@ -34,12 +34,36 @@ class BasicExtension extends AbstractExtension
public function getFilters(): array
{
return [
new TwigFilter('debug', 'debug'),
new TwigFilter('pr', 'pr'),
new TwigFilter('debug', function () {
static $logged = false;
if (!$logged) {
$logged = true;
deprecationWarning('`debug` filter is deprecated, use `dump()` instead.');
}

return debug(...func_get_args());
}),
new TwigFilter('pr', function () {
static $logged = false;
if (!$logged) {
$logged = true;
deprecationWarning('`pr` filter is deprecated, use `dump()` instead.');
}

return pr(...func_get_args());
}),
new TwigFilter('low', 'strtolower'),
new TwigFilter('up', 'strtoupper'),
new TwigFilter('env', 'env'),
new TwigFilter('count', 'count'),
new TwigFilter('count', function () {
static $logged = false;
if (!$logged) {
$logged = true;
deprecationWarning('`count` filter is deprecated, use `length` instead.');
}

return count(...func_get_args());
}),
new TwigFilter('h', 'h'),
new TwigFilter('null', function () {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/Twig/Extension/NumberExtension.php
Expand Up @@ -37,7 +37,7 @@ public function getFilters(): array
return [
new TwigFilter('toReadableSize', 'Cake\I18n\Number::toReadableSize'),
new TwigFilter('toPercentage', 'Cake\I18n\Number::toPercentage'),
new TwigFilter('number_format', 'Cake\I18n\Number::format'),
new TwigFilter('cake_number_format', 'Cake\I18n\Number::format'),
new TwigFilter('formatDelta', 'Cake\I18n\Number::formatDelta'),
new TwigFilter('currency', 'Cake\I18n\Number::currency'),
];
Expand All @@ -51,7 +51,7 @@ public function getFilters(): array
public function getFunctions(): array
{
return [
new TwigFunction('defaultCurrency', 'Cake\I18n\Number::defaultCurrency'),
new TwigFunction('defaultCurrency', 'Cake\I18n\Number::getDefaultCurrency'),
new TwigFunction('number_formatter', 'Cake\I18n\Number::formatter'),
];
}
Expand Down
5 changes: 4 additions & 1 deletion src/View/TwigView.php
Expand Up @@ -212,7 +212,10 @@ protected function initializeExtensions(): void

// Twig core extensions
$twig->addExtension(new StringLoaderExtension());
$twig->addExtension(new DebugExtension());

if (Configure::read('debug', false)) {
$twig->addExtension(new DebugExtension());
}

// CakePHP bridging extensions
$twig->addExtension(new Extension\ArraysExtension());
Expand Down
45 changes: 21 additions & 24 deletions tests/TestCase/Twig/Extension/BasicExtensionTest.php
Expand Up @@ -28,40 +28,37 @@ public function setUp(): void
parent::setUp();
}

public function testFilterDebug()
public function testDeprecatedDebug()
{
$string = 'abc';
$callable = $this->getFilter('debug')->getCallable();
ob_start();
$result = call_user_func_array($callable, [$string, null, false]);
$output = ob_get_clean();
$this->assertSame('abc', $result);
$this->assertSame('
########## DEBUG ##########
\'abc\'
###########################
', $output);

$this->expectDeprecation();
call_user_func_array($callable, ['test']);
}

public function testFilterPr()
public function testDeprecatedPr()
{
$string = 'abc';
$callable = $this->getFilter('pr')->getCallable();
ob_start();
$result = call_user_func_array($callable, [$string]);
$output = ob_get_clean();
$this->assertSame('abc', $result);
$this->assertSame('
abc

', $output);
$this->expectDeprecation();
call_user_func_array($callable, ['test']);
}

public function testFilterCount()
public function testDeprecatedCount()
{
$array = ['a', 'b', 'c'];
$callable = $this->getFilter('count')->getCallable();
$result = call_user_func_array($callable, [$array]);
$this->assertSame(3, $result);

$this->expectDeprecation();
call_user_func_array($callable, ['test']);
}

public function testCount()
{
$this->deprecated(function () {
$array = ['a', 'b', 'c'];
$callable = $this->getFilter('count')->getCallable();
$result = call_user_func_array($callable, [$array]);
$this->assertSame(3, $result);
});
}
}

0 comments on commit 1f95d8f

Please sign in to comment.