MinifyTokenParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
c 0
b 0
f 0
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decideMinifyEnd() 0 3 1
A getTag() 0 3 1
A parse() 0 31 4
1
<?php
2
/**
3
 * Minify plugin for Craft CMS
4
 *
5
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
6
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @license   MIT License https://opensource.org/licenses/MIT
8
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
9
10
namespace nystudio107\minify\twigextensions;
11
12
use Twig\Error\SyntaxError;
13
use Twig\Token;
14
use Twig\TokenParser\AbstractTokenParser;
15
16
/**
17
 * Minify twig token parser
18
 *
19
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   Minify
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
23
class MinifyTokenParser extends AbstractTokenParser
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
29
     * Parses {% minify %}...{% endminify %} tags
30
     *
31
     * @param Token $token
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
32
     *
33
     * @return MinifyNode
34
     * @throws SyntaxError
35
     */
36
    public function parse(Token $token): MinifyNode
37
    {
38
        $lineNo = $token->getLine();
39
        $stream = $this->parser->getStream();
40
41
        $attributes = [
42
            'html' => false,
43
            'css' => false,
44
            'js' => false,
45
        ];
46
47
        if ($stream->test(Token::NAME_TYPE, 'html')) {
48
            $attributes['html'] = true;
49
            $stream->next();
50
        }
51
52
        if ($stream->test(Token::NAME_TYPE, 'css')) {
53
            $attributes['css'] = true;
54
            $stream->next();
55
        }
56
57
        if ($stream->test(Token::NAME_TYPE, 'js')) {
58
            $attributes['js'] = true;
59
            $stream->next();
60
        }
61
62
        $stream->expect(Token::BLOCK_END_TYPE);
63
        $nodes['body'] = $this->parser->subparse([$this, 'decideMinifyEnd'], true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$nodes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $nodes = array(); before regardless.
Loading history...
64
        $stream->expect(Token::BLOCK_END_TYPE);
65
66
        return new MinifyNode($nodes, $attributes, $lineNo, $this->getTag());
67
    }
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @return string
71
     */
72
    public function getTag(): string
73
    {
74
        return 'minify';
75
    }
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
78
     * @param Token $token
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     *
80
     * @return bool
81
     */
82
    public function decideMinifyEnd(Token $token): bool
83
    {
84
        return $token->test('endminify');
85
    }
86
}
87