Skip to content

Commit f498721

Browse files
authored
Merge pull request #57 from cakephp/remove-debug
Updated several filter wrappers.
2 parents 34b94ee + 235f725 commit f498721

File tree

5 files changed

+68
-43
lines changed

5 files changed

+68
-43
lines changed

phpstan.neon

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,5 @@ parameters:
22
level: 6
33
checkMissingIterableValueType: false
44
checkGenericClassInNonGenericObjectType: false
5-
excludes_analyse:
6-
- src/Shell/Task/TwigTemplateTask.php
7-
ignoreErrors:
8-
-
9-
message: "#^Parameter \\#2 \\$callable of class Twig\\\\TwigFilter constructor expects \\(callable\\(\\)\\: mixed\\)\\|null, 'debug' given\\.$#"
10-
count: 1
11-
path: src/Twig/Extension/BasicExtension.php
12-
13-
-
14-
message: "#^Constant CACHE not found\\.$#"
15-
count: 1
16-
path: src/View/TwigView.php
5+
bootstrapFiles:
6+
- tests/bootstrap.php

src/Twig/Extension/BasicExtension.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,36 @@ class BasicExtension extends AbstractExtension
3434
public function getFilters(): array
3535
{
3636
return [
37-
new TwigFilter('debug', 'debug'),
38-
new TwigFilter('pr', 'pr'),
37+
new TwigFilter('debug', function () {
38+
static $logged = false;
39+
if (!$logged) {
40+
$logged = true;
41+
deprecationWarning('`debug` filter is deprecated, use `dump()` instead.');
42+
}
43+
44+
return debug(...func_get_args());
45+
}),
46+
new TwigFilter('pr', function () {
47+
static $logged = false;
48+
if (!$logged) {
49+
$logged = true;
50+
deprecationWarning('`pr` filter is deprecated, use `dump()` instead.');
51+
}
52+
53+
return pr(...func_get_args());
54+
}),
3955
new TwigFilter('low', 'strtolower'),
4056
new TwigFilter('up', 'strtoupper'),
4157
new TwigFilter('env', 'env'),
42-
new TwigFilter('count', 'count'),
58+
new TwigFilter('count', function () {
59+
static $logged = false;
60+
if (!$logged) {
61+
$logged = true;
62+
deprecationWarning('`count` filter is deprecated, use `length` instead.');
63+
}
64+
65+
return count(...func_get_args());
66+
}),
4367
new TwigFilter('h', 'h'),
4468
new TwigFilter('null', function () {
4569
return '';

src/Twig/Extension/NumberExtension.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace Cake\TwigView\Twig\Extension;
2020

21+
use Cake\I18n\Number;
2122
use Twig\Extension\AbstractExtension;
2223
use Twig\TwigFilter;
2324
use Twig\TwigFunction;
@@ -37,7 +38,7 @@ public function getFilters(): array
3738
return [
3839
new TwigFilter('toReadableSize', 'Cake\I18n\Number::toReadableSize'),
3940
new TwigFilter('toPercentage', 'Cake\I18n\Number::toPercentage'),
40-
new TwigFilter('number_format', 'Cake\I18n\Number::format'),
41+
new TwigFilter('cake_number_format', 'Cake\I18n\Number::format'),
4142
new TwigFilter('formatDelta', 'Cake\I18n\Number::formatDelta'),
4243
new TwigFilter('currency', 'Cake\I18n\Number::currency'),
4344
];
@@ -51,8 +52,18 @@ public function getFilters(): array
5152
public function getFunctions(): array
5253
{
5354
return [
54-
new TwigFunction('defaultCurrency', 'Cake\I18n\Number::defaultCurrency'),
55-
new TwigFunction('number_formatter', 'Cake\I18n\Number::formatter'),
55+
new TwigFunction('defaultCurrency', 'Cake\I18n\Number::getDefaultCurrency'),
56+
new TwigFunction('number_formatter', function () {
57+
static $logged = false;
58+
if (!$logged) {
59+
$logged = true;
60+
deprecationWarning(
61+
'`number_formatter()` function is deprecated, use `cake_number_format` filter instead.'
62+
);
63+
}
64+
65+
return Number::formatter(...func_get_args());
66+
}),
5667
];
5768
}
5869
}

src/View/TwigView.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ protected function initializeExtensions(): void
212212

213213
// Twig core extensions
214214
$twig->addExtension(new StringLoaderExtension());
215-
$twig->addExtension(new DebugExtension());
215+
216+
if (Configure::read('debug', false)) {
217+
$twig->addExtension(new DebugExtension());
218+
}
216219

217220
// CakePHP bridging extensions
218221
$twig->addExtension(new Extension\ArraysExtension());

tests/TestCase/Twig/Extension/BasicExtensionTest.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,37 @@ public function setUp(): void
2828
parent::setUp();
2929
}
3030

31-
public function testFilterDebug()
31+
public function testDeprecatedDebug()
3232
{
33-
$string = 'abc';
3433
$callable = $this->getFilter('debug')->getCallable();
35-
ob_start();
36-
$result = call_user_func_array($callable, [$string, null, false]);
37-
$output = ob_get_clean();
38-
$this->assertSame('abc', $result);
39-
$this->assertSame('
40-
########## DEBUG ##########
41-
\'abc\'
42-
###########################
43-
', $output);
34+
35+
$this->expectDeprecation();
36+
call_user_func_array($callable, ['test']);
4437
}
4538

46-
public function testFilterPr()
39+
public function testDeprecatedPr()
4740
{
48-
$string = 'abc';
4941
$callable = $this->getFilter('pr')->getCallable();
50-
ob_start();
51-
$result = call_user_func_array($callable, [$string]);
52-
$output = ob_get_clean();
53-
$this->assertSame('abc', $result);
54-
$this->assertSame('
55-
abc
5642

57-
', $output);
43+
$this->expectDeprecation();
44+
call_user_func_array($callable, ['test']);
5845
}
5946

60-
public function testFilterCount()
47+
public function testDeprecatedCount()
6148
{
62-
$array = ['a', 'b', 'c'];
6349
$callable = $this->getFilter('count')->getCallable();
64-
$result = call_user_func_array($callable, [$array]);
65-
$this->assertSame(3, $result);
50+
51+
$this->expectDeprecation();
52+
call_user_func_array($callable, ['test']);
53+
}
54+
55+
public function testCount()
56+
{
57+
$this->deprecated(function () {
58+
$array = ['a', 'b', 'c'];
59+
$callable = $this->getFilter('count')->getCallable();
60+
$result = call_user_func_array($callable, [$array]);
61+
$this->assertSame(3, $result);
62+
});
6663
}
6764
}

0 commit comments

Comments
 (0)