• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / twig-include-dir / 46692960799

01 Dec 2026 04:35PM UTC coverage: 100.0%. Remained the same
46692960799

push

github

web-flow
Fix the deprecations raised with Twig 3 (#3)

Parse expressions with Parser::parseExpression() where it exists (Twig 3.21),
pass the tag to Node only before Twig 3.12, which sets it itself, and mark
IncludeDirNode as ready for yield (Twig 3.9). Older versions are still
supported.

The invalid directory test expected the surrounding template's name as older
Twig versions spell it and failed with Twig 3.31.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>

9 of 10 new or added lines in 2 files covered. (90.0%)

76 of 76 relevant lines covered (100.0%)

19.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/IncludeDirNode.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace TwigIncludeDir;
6

7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use RecursiveRegexIterator;
10
use RegexIterator;
11
use Twig\Attribute\YieldReady;
12
use Twig\Compiler;
13
use Twig\Error\LoaderError;
14
use Twig\Loader\FilesystemLoader;
15
use Twig\Node\Expression\AbstractExpression;
16
use Twig\Node\Expression\ConstantExpression;
17
use Twig\Node\IncludeNode;
18
use Twig\Node\Node;
19
use Twig\Node\NodeOutputInterface;
20

21
/**
22
 * Class IncludeDirNode
23
 *
24
 * @package TwigIncludeDir
25
 */
26
#[YieldReady]
27
class IncludeDirNode extends Node implements NodeOutputInterface
28
{
29
    public function __construct(
30
        AbstractExpression $expr,
31
        ?AbstractExpression $variables = null,
32
        bool $recursive = false,
33
        bool $only = false,
34
        int $lineno = 0,
35
        string $tag = ''
36
    ) {
37
        $nodes = ['expr' => $expr];
25 ✔
38
        if (null !== $variables) {
25 ✔
39
            $nodes['variables'] = $variables;
9 ✔
40
        }
41

42
        $arguments = [
25 ✔
43
            $nodes,
25 ✔
44
            [
25 ✔
45
                'recursive' => $recursive,
25 ✔
46
                'only' => $only
25 ✔
47
            ],
25 ✔
48
            $lineno,
25 ✔
49
        ];
25 ✔
50

51
        // since Twig 3.12 the parser sets the tag itself and passing it is deprecated
52
        if (!method_exists(Node::class, 'setNodeTag')) {
25 ✔
NEW
53
            $arguments[] = $tag;
12 ✔
54
        }
55

56
        parent::__construct(...$arguments);
25 ✔
57
    }
58

59
    /**
60
     * @param Compiler $compiler
61
     *
62
     * @throws LoaderError
63
     */
64
    public function compile(Compiler $compiler): void
65
    {
66
        $loader = $compiler->getEnvironment()->getLoader();
25 ✔
67

68
        if (!$loader instanceof FilesystemLoader) {
25 ✔
69
            throw new LoaderError('IncludeDir is only supported for filesystem loader!');
2 ✔
70
        }
71

72
        $includePath = '';
23 ✔
73
        $loaderPath  = '';
23 ✔
74
        foreach ($loader->getPaths() as $path) {
23 ✔
75
            if (is_dir($path . $this->getNode('expr')->getAttribute('value'))) {
23 ✔
76
                $includePath = $path . $this->getNode('expr')->getAttribute('value');
21 ✔
77
                $loaderPath  = $path;
21 ✔
78
            }
79
        }
80

81
        if (empty($includePath)) {
23 ✔
82
            throw new LoaderError(
2 ✔
83
                sprintf(
2 ✔
84
                    'Unable to find template "%s" (looked into: %s).',
2 ✔
85
                    $this->getNode('expr')->getAttribute('value'),
2 ✔
86
                    implode(', ', $loader->getPaths())
2 ✔
87
                )
2 ✔
88
            );
2 ✔
89
        }
90

91
        if ($this->getAttribute('recursive')) {
21 ✔
92
            $directory = new RecursiveDirectoryIterator($includePath);
9 ✔
93
            $iterator = new RecursiveIteratorIterator($directory);
9 ✔
94
            $foundFiles = new RegexIterator($iterator, '/^.+\.twig$/i', RecursiveRegexIterator::GET_MATCH);
9 ✔
95

96
            $files = [];
9 ✔
97
            foreach ($foundFiles as $file) {
9 ✔
98
                $files[] = $file[0];
9 ✔
99
            }
100
        } else {
101
            $files = glob($includePath . '/*.twig');
14 ✔
102
        }
103

104
        sort($files);
21 ✔
105

106
        foreach ($files as $file) {
21 ✔
107
            $file = str_replace(DIRECTORY_SEPARATOR, '/', str_replace($loaderPath, '', $file));
21 ✔
108
            $template = new IncludeNode(
21 ✔
109
                new ConstantExpression($file, $this->lineno),
21 ✔
110
                $this->hasNode('variables') ? $this->getNode('variables') : null,
21 ✔
111
                $this->getAttribute('only'),
21 ✔
112
                false,
21 ✔
113
                $this->lineno
21 ✔
114
            );
21 ✔
115

116
            $template->compile($compiler);
21 ✔
117
        }
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc