Issues (77)

src/Node/Expression/EmptyCoalesceExpression.php (21 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
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
Missing short description in doc comment
Loading history...
19
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   EmptyCoalesce
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 *
23
 */
0 ignored issues
show
Additional blank lines found at end of doc comment
Loading history...
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
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
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
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
Space after closing parenthesis of function call prohibited
Loading history...
77
        ;
78
    }
79
}
80