Skip to content

Commit 414d0cf

Browse files
authored
Merge pull request #7 from xvilo/twig-3-support
Twig 3 support
2 parents ff2717d + af08ddd commit 414d0cf

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor/
22
composer.lock
3+
.phpunit.result.cache
34

45
# Created by https://www.gitignore.io/api/vim
56

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ cache:
88
- vendor
99

1010
php:
11-
- 7.1
12-
- 7.2
13-
- 7.3
11+
- 7.2 # EOL: 20 Nov 2020
12+
- 7.3 # EOL: 6 Dec 2021
13+
- 7.4 # EOL: 28 Nov 2022
1414

1515
matrix:
1616
fast_finish: true

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"psr-4": { "Sserbin\\TwigLinter\\Tests\\": "tests/"}
1717
},
1818
"require": {
19-
"php": "~7.1",
19+
"php": "~7.2",
2020
"symfony/console": "^4.1",
2121
"symfony/finder": "^4.1",
22-
"twig/twig": "^2.5",
22+
"twig/twig": "^2.5 || ^3",
2323
"ocramius/package-versions": "^1.3"
2424
},
2525
"require-dev": {

src/Command/LintCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2+
23
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Symfony package.
57
*

src/StubEnvironment.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Sserbin\TwigLinter;
@@ -11,9 +12,10 @@
1112
class StubEnvironment extends Environment
1213
{
1314
/**
14-
* {@inheritdoc}
15+
* @param string $name
16+
* @psalm-suppress ImplementedReturnTypeMismatch
1517
*/
16-
public function getFilter($name)
18+
public function getFilter($name): ?TwigFilter
1719
{
1820
/**
1921
* @var string[]
@@ -24,7 +26,7 @@ public function getFilter($name)
2426

2527
if ($isDefault) { // don't attempt to stub twig's builtin filter
2628
/** @psalm-suppress InternalMethod */
27-
return parent::getFilter($name);
29+
return parent::getFilter($name) ?: null;
2830
}
2931

3032
return new TwigFilter((string)$name, $this->noop(), [
@@ -33,9 +35,10 @@ public function getFilter($name)
3335
}
3436

3537
/**
36-
* {@inheritdoc}
38+
* @param string $name
39+
* @psalm-suppress ImplementedReturnTypeMismatch
3740
*/
38-
public function getFunction($name)
41+
public function getFunction($name): ?TwigFunction
3942
{
4043
/**
4144
* @var string[]
@@ -46,7 +49,7 @@ public function getFunction($name)
4649

4750
if ($isDefault) { // don't attempt to stub twig's builtin function
4851
/** @psalm-suppress InternalMethod */
49-
return parent::getFunction($name);
52+
return parent::getFunction($name) ?: null;
5053
}
5154

5255
return new TwigFunction((string)$name, $this->noop(), [
@@ -55,9 +58,10 @@ public function getFunction($name)
5558
}
5659

5760
/**
58-
* {@inheritdoc}
61+
* @param string $name
62+
* @psalm-suppress ImplementedReturnTypeMismatch
5963
*/
60-
public function getTest($name)
64+
public function getTest($name): ?TwigTest
6165
{
6266
/**
6367
* @var string[]
@@ -68,7 +72,16 @@ public function getTest($name)
6872

6973
if ($isDefault) { // don't attempt to stub twig's builtin test
7074
/** @psalm-suppress InternalMethod */
71-
return parent::getTest($name);
75+
$parentTest = parent::getTest($name);
76+
77+
if ($parentTest instanceof TwigTest) {
78+
return $parentTest;
79+
}
80+
81+
// In twig 2.x this can return `false`.
82+
// Lets just force it here as null because
83+
// of the added typehint for Twig 3.x
84+
return null;
7285
}
7386

7487
return new TwigTest((string)$name, $this->noop(), [
@@ -80,13 +93,15 @@ private function noop(): callable
8093
{
8194
/**
8295
* @param mixed $_
96+
* @param array $arg
8397
*/
8498
return function ($_ = null, array $arg = []): void {
8599
};
86100
}
87101

88102
/**
89103
* @param string[] $list
104+
* @return bool
90105
*/
91106
private function listContainsSubstring(array $list, string $needle): bool
92107
{

tests/LintCommandTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* This file is part of the Symfony package.
45
*
@@ -10,14 +11,12 @@
1011
namespace Sserbin\TwigLinter\Tests;
1112

1213
use PHPUnit\Framework\TestCase;
13-
14+
use RuntimeException;
1415
use Symfony\Component\Console\Application;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use Symfony\Component\Console\Tester\CommandTester;
17-
1818
use Twig\Environment;
1919
use Twig\Loader\FilesystemLoader;
20-
2120
use Sserbin\TwigLinter\Command\LintCommand;
2221

2322
class LintCommandTest extends TestCase
@@ -36,7 +35,7 @@ public function testLintCorrectFile(): void
3635
);
3736

3837
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
39-
$this->assertContains('OK in', trim($tester->getDisplay()));
38+
$this->assertStringContainsString('OK in', trim($tester->getDisplay()));
4039
}
4140

4241
public function testLintIncorrectFile(): void
@@ -50,11 +49,9 @@ public function testLintIncorrectFile(): void
5049
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
5150
}
5251

53-
/**
54-
* @expectedException \RuntimeException
55-
*/
5652
public function testLintFileNotReadable(): void
5753
{
54+
$this->expectException(RuntimeException::class);
5855
$tester = $this->createCommandTester();
5956
$filename = $this->createFile('');
6057
unlink($filename);

0 commit comments

Comments
 (0)