Passed
Push — v1 ( 514194...e06840 )
by Andrew
06:20
created

EmptyCoalesceExpression::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Empty Coalesce plugin for Craft CMS 3.x
4
 *
5
 * Empty Coalesce adds the ??? operator to Twig that will return the first thing
6
 * that is defined, not null, and not empty.
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
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...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\emptycoalesce\Node\Expression;
13
14
use Twig\Compiler;
15
use Twig\Node\Expression\AbstractExpression;
16
use Twig\Node\Node;
17
18
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
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   EmptyCoalesce
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.0.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
 *
23
 */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
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...
24
class EmptyCoalesceExpression extends AbstractExpression
25
{
26
    /**
27
     * Checks if a variable is empty.
28
     *
29
     *    {# evaluates to true if the foo variable is null, false, or the empty string #}
30
     *    {% if foo is empty %}
31
     *        {# ... #}
32
     *    {% endif %}
33
     *
34
     * @param mixed $value A variable
35
     *
36
     * @return bool true if the value is empty, false otherwise
37
     */
38
    public static function empty($value): bool
39
    {
40
        if ($value instanceof \Countable) {
41
            return 0 == \count($value);
42
        }
43
44
        if (\is_object($value) && method_exists($value, '__toString')) {
45
            return '' === (string) $value;
46
        }
47
48
        return '' === $value || false === $value || null === $value || [] === $value;
49
    }
50
51
    public function __construct(Node $left, Node $right, $lineno)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
52
    {
53
        $left->setAttribute('ignore_strict_check', true);
54
        $left->setAttribute('is_defined_test', false);
55
        $right->setAttribute('ignore_strict_check', true);
56
        $right->setAttribute('is_defined_test', false);
57
        parent::__construct(
58
            ['left' => $left, 'right' => $right],
59
            ['ignore_strict_check' => true, 'is_defined_test' => false],
60
            $lineno
61
        );
62
    }
63
64
    public function compile(Compiler $compiler)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function compile()
Loading history...
65
    {
66
        //$this->getNode('expr1')->setAttribute('always_defined', true);
67
        $compiler
68
            ->raw('(('.self::class.'::empty(')
69
            ->subcompile($this->getNode('left'))
70
            ->raw(') ? null : ')
71
            ->subcompile($this->getNode('left'))
72
            ->raw(') ?? ('.self::class.'::empty(')
73
            ->subcompile($this->getNode('right'))
74
            ->raw(') ? null : ')
75
            ->subcompile($this->getNode('right'))
76
            ->raw('))')
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
77
        ;
78
    }
79
}
80
81
class_alias('nystudio107\emptycoalesce\Node\Expression\EmptyCoalesceExpression', 'Twig_Node_Expression_EmptyCoalesce');
82