Completed
Push — master ( 9ad54b...895d73 )
by Federico
03:21
created

CompilerFacade::terminateMixinBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Jade\Compiler;
4
5
/**
6
 * Class Jade CompilerFacade.
7
 * Expose methods available from compiled jade tempaltes.
8
 */
9
abstract class CompilerFacade extends ValuesCompiler
10
{
11
    protected static $mixinBlocks = array();
12
13
    /**
14
     * Record a closure as a mixin block during execution jade template time.
15
     *
16
     * @param string  mixin name
17
     * @param string  mixin block treatment
18
     */
19
    public static function recordMixinBlock($name, $func = null)
20
    {
21
        if (!isset(static::$mixinBlocks[$name])) {
22
            static::$mixinBlocks[$name] = array();
23
        }
24
        array_push(static::$mixinBlocks[$name], $func);
25
    }
26
27
    /**
28
     * Record a closure as a mixin block during execution jade template time.
29
     *
30
     * @param string  mixin name
31
     * @param string  mixin block treatment
32
     */
33 View Code Duplication
    public static function callMixinBlock($name, $attributes = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) {
36
            $func = end($mixinBlocks);
37
            if (is_callable($func)) {
38
                $func($attributes);
39
            }
40
        }
41
    }
42
43
    /**
44
     * Record a closure as a mixin block during execution jade template time
45
     * and propagate variables.
46
     *
47
     * @param string  mixin name
48
     * @param &array  variables handler propagated from parent scope
49
     * @param string  mixin block treatment
50
     */
51 View Code Duplication
    public static function callMixinBlockWithVars($name, &$varHandler, $attributes = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) {
54
            $func = end($mixinBlocks);
55
            if (is_callable($func)) {
56
                $func($varHandler, $attributes);
57
            }
58
        }
59
    }
60
61
    /**
62
     * End of the record of the mixin block.
63
     *
64
     * @param string  mixin name
65
     */
66
    public static function terminateMixinBlock($name)
67
    {
68
        if (isset(static::$mixinBlocks[$name])) {
69
            array_pop(static::$mixinBlocks);
70
        }
71
    }
72
73
    /**
74
     * Get property from object.
75
     *
76
     * @param object $object source object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @param mixed  $key    key to retrive from the object or the array
78
     *
79
     * @return mixed
80
     */
81
    public static function getPropertyFromObject($anything, $key)
82
    {
83
        return isset($anything->$key)
84
            ? $anything->$key
85
            : (method_exists($anything, $method = 'get' . ucfirst($key))
86
                ? $anything->$method()
87
                : (method_exists($anything, $key)
88
                    ? array($anything, $key)
89
                    : null
90
                )
91
            );
92
    }
93
94
    /**
95
     * Get property from object or entry from array.
96
     *
97
     * @param object|array $anything source
98
     * @param mixed        $key      key to retrive from the object or the array
99
     *
100
     * @return mixed
101
     */
102
    public static function getPropertyFromAnything($anything, $key)
103
    {
104
        return is_array($anything)
105
            ? (isset($anything[$key])
106
                ? $anything[$key]
107
                : null
108
            ) : (is_object($anything)
109
                ? static::getPropertyFromObject($anything, $key)
110
                : null
111
            );
112
    }
113
114
    /**
115
     * Merge given attributes such as tag attributes with mixin attributes.
116
     *
117
     * @param array $attributes
118
     * @param array $mixinAttributes
119
     *
120
     * @return array
121
     */
122
    public static function withMixinAttributes($attributes, $mixinAttributes)
123
    {
124
        foreach ($mixinAttributes as $attribute) {
125
            if ($attribute['name'] === 'class') {
126
                $value = static::joinAny($attribute['value']);
127
                $attributes['class'] = empty($attributes['class'])
128
                    ? $value
129
                    : static::joinAny($attributes['class']) . ' ' . $value;
130
            }
131
        }
132
        if (isset($attributes['class'])) {
133
            $attributes['class'] = implode(' ', array_unique(explode(' ', $attributes['class'])));
134
        }
135
136
        return $attributes;
137
    }
138
139
    /**
140
     * Display a list of attributes with the given quote character in HTML.
141
     *
142
     * @param array  $attributes
143
     * @param string $quote
144
     */
145
    public static function displayAttributes($attributes, $quote, $terse)
146
    {
147
        if (is_array($attributes) || $attributes instanceof Traversable) {
0 ignored issues
show
Bug introduced by
The class Jade\Compiler\Traversable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
148
            foreach ($attributes as $key => $value) {
149
                if ($key !== 'class' && $value !== false && $value !== 'null') {
150
                    if ($value === true) {
151
                        echo ' ' . $key . ($terse ? '' : '=' . $quote . $key . $quote);
152
                        continue;
153
                    }
154
                    echo ' ' . $key . '=' . $quote . htmlspecialchars($value) . $quote;
155
                }
156
            }
157
        }
158
    }
159
160
    /**
161
     * Return true if the given value can be display
162
     * (null or false should not be displayed in the output HTML).
163
     *
164
     * @param $value
165
     *
166
     * @return bool
167
     */
168
    public static function isDisplayable($value)
169
    {
170
        return !is_null($value) && $value !== false;
171
    }
172
}
173