Skip to content

Commit babf95d

Browse files
committed
refactor: by Rector
1 parent 623aa90 commit babf95d

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

src/CI4Twig/Twig.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
namespace Kenjis\CI4Twig;
1111

12+
use Config\Services;
13+
use Twig\Environment;
14+
use Twig\Error\LoaderError;
15+
use Twig\Error\RuntimeError;
16+
use Twig\Error\SyntaxError;
17+
use Twig\Extension\DebugExtension;
18+
use Twig\Loader\FilesystemLoader;
19+
use Twig\TwigFunction;
20+
1221
class Twig
1322
{
1423
/**
@@ -26,7 +35,7 @@ class Twig
2635
/**
2736
* @var array Functions to add to Twig
2837
*/
29-
private $functions_asis = [
38+
private array $functions_asis = [
3039
'base_url',
3140
'site_url',
3241
];
@@ -36,7 +45,7 @@ class Twig
3645
*
3746
* @see https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
3847
*/
39-
private $functions_safe = [
48+
private array $functions_safe = [
4049
'form_open',
4150
'form_close',
4251
'form_error',
@@ -50,15 +59,12 @@ class Twig
5059
/**
5160
* @var bool Whether functions are added or not
5261
*/
53-
private $functions_added = false;
62+
private bool $functions_added = false;
5463

55-
/**
56-
* @var \Twig\Environment|null
57-
*/
58-
private $twig;
64+
private ?Environment $twig = null;
5965

6066
/**
61-
* @var \Twig\Loader\FilesystemLoader
67+
* @var FilesystemLoader
6268
*/
6369
private $loader;
6470

@@ -112,13 +118,13 @@ protected function createTwig()
112118
}
113119

114120
if ($this->loader === null) {
115-
$this->loader = new \Twig\Loader\FilesystemLoader($this->paths);
121+
$this->loader = new FilesystemLoader($this->paths);
116122
}
117123

118-
$twig = new \Twig\Environment($this->loader, $this->config);
124+
$twig = new Environment($this->loader, $this->config);
119125

120126
if ($this->config['debug']) {
121-
$twig->addExtension(new \Twig\Extension\DebugExtension());
127+
$twig->addExtension(new DebugExtension());
122128
}
123129

124130
$this->twig = $twig;
@@ -147,9 +153,9 @@ public function addGlobal($name, $value)
147153
* @param string $view Template filename without `.twig`
148154
* @param array $params Array of parameters to pass to the template
149155
*
150-
* @throws \Twig\Error\LoaderError
151-
* @throws \Twig\Error\RuntimeError
152-
* @throws \Twig\Error\SyntaxError
156+
* @throws LoaderError
157+
* @throws RuntimeError
158+
* @throws SyntaxError
153159
*/
154160
public function display($view, $params = [])
155161
{
@@ -162,9 +168,9 @@ public function display($view, $params = [])
162168
* @param string $view Template filename without `.twig`
163169
* @param array $params Array of parameters to pass to the template
164170
*
165-
* @throws \Twig\Error\LoaderError
166-
* @throws \Twig\Error\RuntimeError
167-
* @throws \Twig\Error\SyntaxError
171+
* @throws LoaderError
172+
* @throws RuntimeError
173+
* @throws SyntaxError
168174
*/
169175
public function render($view, $params = []): string
170176
{
@@ -189,7 +195,7 @@ protected function addFunctions()
189195
foreach ($this->functions_asis as $function) {
190196
if (function_exists($function)) {
191197
$this->twig->addFunction(
192-
new \Twig\TwigFunction(
198+
new TwigFunction(
193199
$function,
194200
$function
195201
)
@@ -201,7 +207,7 @@ protected function addFunctions()
201207
foreach ($this->functions_safe as $function) {
202208
if (function_exists($function)) {
203209
$this->twig->addFunction(
204-
new \Twig\TwigFunction(
210+
new TwigFunction(
205211
$function,
206212
$function,
207213
['is_safe' => ['html']]
@@ -213,7 +219,7 @@ protected function addFunctions()
213219
// customized functions
214220
if (function_exists('anchor')) {
215221
$this->twig->addFunction(
216-
new \Twig\TwigFunction(
222+
new TwigFunction(
217223
'anchor',
218224
[$this, 'safe_anchor'],
219225
['is_safe' => ['html']]
@@ -222,7 +228,7 @@ protected function addFunctions()
222228
}
223229

224230
$this->twig->addFunction(
225-
new \Twig\TwigFunction(
231+
new TwigFunction(
226232
'validation_list_errors',
227233
[$this, 'validation_list_errors'],
228234
['is_safe' => ['html']]
@@ -256,10 +262,10 @@ public function safe_anchor(
256262

257263
public function validation_list_errors(): string
258264
{
259-
return \Config\Services::validation()->listErrors();
265+
return Services::validation()->listErrors();
260266
}
261267

262-
public function getTwig(): \Twig\Environment
268+
public function getTwig(): Environment
263269
{
264270
$this->createTwig();
265271

tests/CI4Twig/ReflectionHelper.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ public static function getPrivateMethodInvoker($obj, $method)
2121
$ref_method->setAccessible(true);
2222
$obj = (gettype($obj) === 'object') ? $obj : null;
2323

24-
return static function () use ($obj, $ref_method) {
25-
$args = func_get_args();
26-
27-
return $ref_method->invokeArgs($obj, $args);
28-
};
24+
return static fn (...$args) => $ref_method->invokeArgs($obj, $args);
2925
}
3026

3127
protected static function getAccessibleRefProperty($obj, $property)
3228
{
33-
if (is_object($obj)) {
34-
$ref_class = new ReflectionObject($obj);
35-
} else {
36-
$ref_class = new ReflectionClass($obj);
37-
}
29+
$ref_class = is_object($obj) ? new ReflectionObject($obj) : new ReflectionClass($obj);
3830

3931
$ref_property = $ref_class->getProperty($property);
4032
$ref_property->setAccessible(true);

tests/CI4Twig/TwigHelperTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Kenjis\CI4Twig;
44

5+
use Twig\Environment;
6+
use Twig\Loader\ArrayLoader;
7+
58
/**
69
* @internal
710
*/
811
final class TwigHelperTest extends TestCase
912
{
10-
private $twig;
13+
private Environment $twig;
1114

1215
public static function setUpBeforeClass(): void
1316
{
@@ -20,7 +23,7 @@ protected function setUp(): void
2023
{
2124
$twig = new Twig();
2225

23-
$loader = new \Twig\Loader\ArrayLoader(
26+
$loader = new ArrayLoader(
2427
[
2528
'base_url' => '{{ base_url(\'"><s>abc</s><a name="test\') }}',
2629
'site_url' => '{{ site_url(\'"><s>abc</s><a name="test\') }}',

0 commit comments

Comments
 (0)