Skip to content

Commit 3edde02

Browse files
committed
style: fix coding style
1 parent 39711d8 commit 3edde02

File tree

6 files changed

+109
-92
lines changed

6 files changed

+109
-92
lines changed

src/CI4Twig/Twig.php

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
/**
33
* Part of CodeIgniter Simple and Secure Twig
44
*
5-
* @author Kenji Suzuki <https://github.com/kenjis>
6-
* @license MIT License
7-
* @copyright 2015 Kenji Suzuki
8-
* @link https://github.com/kenjis/codeigniter-ss-twig
5+
* @license MIT License
6+
* @copyright 2015 Kenji Suzuki
7+
* @see https://github.com/kenjis/codeigniter-ss-twig
98
*/
109

1110
namespace Kenjis\CI4Twig;
@@ -19,6 +18,7 @@ class Twig
1918

2019
/**
2120
* @var array Twig Environment Options
21+
*
2222
* @see https://twig.symfony.com/doc/3.x/api.html#environment-options
2323
*/
2424
private $config = [];
@@ -33,6 +33,7 @@ class Twig
3333

3434
/**
3535
* @var array Functions with `is_safe` option
36+
*
3637
* @see https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
3738
*/
3839
private $functions_safe = [
@@ -42,8 +43,8 @@ class Twig
4243
'form_hidden',
4344
'set_value',
4445
'csrf_field',
45-
// 'form_open_multipart', 'form_upload', 'form_submit', 'form_dropdown',
46-
// 'set_radio', 'set_select', 'set_checkbox',
46+
// 'form_open_multipart', 'form_upload', 'form_submit', 'form_dropdown',
47+
// 'set_radio', 'set_select', 'set_checkbox',
4748
];
4849

4950
/**
@@ -64,21 +65,19 @@ class Twig
6465
public function __construct($params = [])
6566
{
6667
if (isset($params['functions'])) {
67-
$this->functions_asis =
68-
array_unique(
69-
array_merge($this->functions_asis, $params['functions'])
70-
);
68+
$this->functions_asis = array_unique(
69+
array_merge($this->functions_asis, $params['functions'])
70+
);
7171
unset($params['functions']);
7272
}
7373

7474
if (isset($params['functions_safe'])) {
75-
$this->functions_safe =
76-
array_unique(
77-
array_merge(
78-
$this->functions_safe,
79-
$params['functions_safe']
80-
)
81-
);
75+
$this->functions_safe = array_unique(
76+
array_merge(
77+
$this->functions_safe,
78+
$params['functions_safe']
79+
)
80+
);
8281
unset($params['functions_safe']);
8382
}
8483

@@ -91,8 +90,8 @@ public function __construct($params = [])
9190

9291
// default Twig config
9392
$this->config = [
94-
'cache' => WRITEPATH . 'cache/twig',
95-
'debug' => ENVIRONMENT !== 'production',
93+
'cache' => WRITEPATH . 'cache/twig',
94+
'debug' => ENVIRONMENT !== 'production',
9695
'autoescape' => 'html',
9796
];
9897

@@ -133,8 +132,8 @@ protected function setLoader($loader)
133132
/**
134133
* Registers a Global
135134
*
136-
* @param string $name The global name
137-
* @param mixed $value The global value
135+
* @param string $name The global name
136+
* @param mixed $value The global value
138137
*/
139138
public function addGlobal($name, $value)
140139
{
@@ -145,8 +144,9 @@ public function addGlobal($name, $value)
145144
/**
146145
* Renders Twig Template and Outputs
147146
*
148-
* @param string $view Template filename without `.twig`
149-
* @param array $params Array of parameters to pass to the template
147+
* @param string $view Template filename without `.twig`
148+
* @param array $params Array of parameters to pass to the template
149+
*
150150
* @throws \Twig\Error\LoaderError
151151
* @throws \Twig\Error\RuntimeError
152152
* @throws \Twig\Error\SyntaxError
@@ -159,9 +159,9 @@ public function display($view, $params = [])
159159
/**
160160
* Renders Twig Template and Returns as String
161161
*
162-
* @param string $view Template filename without `.twig`
163-
* @param array $params Array of parameters to pass to the template
164-
* @return string
162+
* @param string $view Template filename without `.twig`
163+
* @param array $params Array of parameters to pass to the template
164+
*
165165
* @throws \Twig\Error\LoaderError
166166
* @throws \Twig\Error\RuntimeError
167167
* @throws \Twig\Error\SyntaxError
@@ -174,6 +174,7 @@ public function render($view, $params = []): string
174174
$this->addFunctions();
175175

176176
$view = $view . '.twig';
177+
177178
return $this->twig->render($view, $params);
178179
}
179180

@@ -234,18 +235,18 @@ protected function addFunctions()
234235
/**
235236
* @param string $uri
236237
* @param string $title
237-
* @param array $attributes only array is acceptable
238-
* @return string
238+
* @param array $attributes only array is acceptable
239239
*/
240240
public function safe_anchor(
241241
$uri = '',
242242
$title = '',
243243
$attributes = []
244244
): string {
245-
$uri = esc($uri, 'url');
245+
$uri = esc($uri, 'url');
246246
$title = esc($title);
247247

248248
$new_attr = [];
249+
249250
foreach ($attributes as $key => $val) {
250251
$new_attr[esc($key)] = $val;
251252
}
@@ -258,12 +259,10 @@ public function validation_list_errors(): string
258259
return \Config\Services::validation()->listErrors();
259260
}
260261

261-
/**
262-
* @return \Twig\Environment
263-
*/
264262
public function getTwig(): \Twig\Environment
265263
{
266264
$this->createTwig();
265+
267266
return $this->twig;
268267
}
269268
}

tests/CI4Twig/ReflectionHelper.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace Kenjis\CI4Twig;
44

55
use Closure;
6+
use ReflectionClass;
67
use ReflectionMethod;
78
use ReflectionObject;
8-
use ReflectionClass;
99

1010
class ReflectionHelper
1111
{
1212
/**
13-
* @param object|string $obj object or class name
14-
* @param string $method method name
13+
* @param object|string $obj object or class name
14+
* @param string $method method name
15+
*
1516
* @return closure
1617
*/
1718
public static function getPrivateMethodInvoker($obj, $method)
@@ -20,8 +21,9 @@ public static function getPrivateMethodInvoker($obj, $method)
2021
$ref_method->setAccessible(true);
2122
$obj = (gettype($obj) === 'object') ? $obj : null;
2223

23-
return function () use ($obj, $ref_method) {
24+
return static function () use ($obj, $ref_method) {
2425
$args = func_get_args();
26+
2527
return $ref_method->invokeArgs($obj, $args);
2628
};
2729
}
@@ -41,9 +43,9 @@ protected static function getAccessibleRefProperty($obj, $property)
4143
}
4244

4345
/**
44-
* @param object|string $obj object or class name
45-
* @param string $property property name
46-
* @param mixed $value value
46+
* @param object|string $obj object or class name
47+
* @param string $property property name
48+
* @param mixed $value value
4749
*/
4850
public static function setPrivateProperty($obj, $property, $value)
4951
{
@@ -52,13 +54,15 @@ public static function setPrivateProperty($obj, $property, $value)
5254
}
5355

5456
/**
55-
* @param object|string $obj object or class name
56-
* @param string $property property name
57+
* @param object|string $obj object or class name
58+
* @param string $property property name
59+
*
5760
* @return mixed value
5861
*/
5962
public static function getPrivateProperty($obj, $property)
6063
{
6164
$ref_property = self::getAccessibleRefProperty($obj, $property);
65+
6266
return $ref_property->getValue($obj);
6367
}
6468
}

tests/CI4Twig/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Kenjis\CI4Twig;
44

5+
/**
6+
* @internal
7+
*
8+
* @no-final
9+
*/
510
class TestCase extends \PHPUnit\Framework\TestCase
611
{
712
}

tests/CI4Twig/TwigHelperTest.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Kenjis\CI4Twig;
44

5-
class TwigHelperTest extends TestCase
5+
/**
6+
* @internal
7+
*/
8+
final class TwigHelperTest extends TestCase
69
{
710
private $twig;
811

@@ -13,15 +16,15 @@ public static function setUpBeforeClass(): void
1316
helper('url');
1417
}
1518

16-
public function setUp(): void
19+
protected function setUp(): void
1720
{
1821
$twig = new Twig();
1922

2023
$loader = new \Twig\Loader\ArrayLoader(
2124
[
2225
'base_url' => '{{ base_url(\'"><s>abc</s><a name="test\') }}',
2326
'site_url' => '{{ site_url(\'"><s>abc</s><a name="test\') }}',
24-
'anchor' => '{{ anchor(uri, title, attributes) }}',
27+
'anchor' => '{{ anchor(uri, title, attributes) }}',
2528
]
2629
);
2730
$setLoader = ReflectionHelper::getPrivateMethodInvoker(
@@ -45,45 +48,45 @@ public function setUp(): void
4548
$this->twig = $twig->getTwig();
4649
}
4750

48-
public function test_anchor()
51+
public function testAnchor()
4952
{
5053
$actual = $this->twig->render(
5154
'anchor',
5255
[
53-
'uri' => 'news/local/123',
54-
'title' => 'My News',
55-
'attributes' => ['title' => 'The best news!']
56+
'uri' => 'news/local/123',
57+
'title' => 'My News',
58+
'attributes' => ['title' => 'The best news!'],
5659
]
5760
);
58-
// $expected = '<a href="http://localhost/index.php/news/local/123" title="The best news!">My News</a>'; // CI3
61+
// $expected = '<a href="http://localhost/index.php/news/local/123" title="The best news!">My News</a>'; // CI3
5962
$expected = '<a href="http://localhost/index.php/news/local/123" title="The best news!">My News</a>';
60-
$this->assertEquals($expected, $actual);
63+
$this->assertSame($expected, $actual);
6164

6265
$actual = $this->twig->render(
6366
'anchor',
6467
[
65-
'uri' => 'news/local/123',
66-
'title' => '<s>abc</s>',
67-
'attributes' => ['<s>name</s>' => '<s>val</s>']
68+
'uri' => 'news/local/123',
69+
'title' => '<s>abc</s>',
70+
'attributes' => ['<s>name</s>' => '<s>val</s>'],
6871
]
6972
);
7073
$expected = '<a href="http://localhost/index.php/news/local/123" &lt;s&gt;name&lt;/s&gt;="&lt;s&gt;val&lt;/s&gt;">&lt;s&gt;abc&lt;/s&gt;</a>';
71-
$this->assertEquals($expected, $actual);
74+
$this->assertSame($expected, $actual);
7275
}
7376

74-
public function test_base_url()
77+
public function testBaseUrl()
7578
{
7679
$actual = $this->twig->render('base_url');
77-
// $expected = 'http://localhost/&quot;&gt;&lt;s&gt;abc&lt;/s&gt;&lt;a name=&quot;test'; // CI3
80+
// expected = 'http://localhost/&quot;&gt;&lt;s&gt;abc&lt;/s&gt;&lt;a name=&quot;test'; // CI3
7881
$expected = 'http://localhost/%22%3E%3Cs%3Eabc%3C/s%3E%3Ca%20name=%22test';
79-
$this->assertEquals($expected, $actual);
82+
$this->assertSame($expected, $actual);
8083
}
8184

82-
public function test_site_url()
85+
public function testSiteUrl()
8386
{
8487
$actual = $this->twig->render('site_url');
85-
// $expected = 'http://localhost/index.php/&quot;&gt;&lt;s&gt;abc&lt;/s&gt;&lt;a name=&quot;test'; // CI3
88+
// $expected = 'http://localhost/index.php/&quot;&gt;&lt;s&gt;abc&lt;/s&gt;&lt;a name=&quot;test'; // CI3
8689
$expected = 'http://localhost/index.php/%22%3E%3Cs%3Eabc%3C/s%3E%3Ca%20name=%22test';
87-
$this->assertEquals($expected, $actual);
90+
$this->assertSame($expected, $actual);
8891
}
8992
}

0 commit comments

Comments
 (0)