func.php ➔ componentFunc()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 4
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\RenderContext;
4
5
use League\Plates;
6
use League\Plates\Exception\FuncException;
7
8
function componentFunc($insert = null) {
9 32
    $insert = $insert ?: insertFunc();
10
    return startBufferFunc(function(FuncArgs $args) use ($insert) {
11 12
        if ($args->template()->get('component_slot_data') !== null) {
12 4
            throw new FuncException('Cannot nest component func calls.');
13
        }
14
15 12
        $args->template()->with('component_slot_data', []);
16
        return function($contents) use ($insert, $args) {
17 8
            list($name, $data) = $args->args;
18
19 8
            $data = array_merge(
20 8
                $data ?: [],
21 8
                ['slot' => $contents],
22 8
                $args->template()->get('component_slot_data')
23
            );
24
25 8
            $insert($args->withArgs([$name, $data]));
26
27 8
            $args->template()->with('component_slot_data', null);
28 12
        };
29 32
    });
30
}
31
32
function slotFunc() {
33
    return startBufferFunc(function(FuncArgs $args) {
34 8
        if ($args->template()->get('component_slot_data') === null) {
35 4
            throw new FuncException('Cannot call slot func outside of component definition.');
36
        }
37
38
        return function($contents) use ($args) {
39 4
            $slot_data = $args->template()->get('component_slot_data');
40 4
            $slot_data[$args->args[0]] = $contents;
41 4
            $args->template()->with('component_slot_data', $slot_data);
42 4
        };
43 32
    });
44
}
45
46
function startBufferFunc(callable $create_callback) {
47
    return function(FuncArgs $args) use ($create_callback) {
48 44
        $buffer_stack = $args->template()->get('buffer_stack') ?: [];
49
50 44
        ob_start();
51 44
        $buffer_stack[] = [ob_get_level(), $create_callback($args)];
52
53 40
        $args->template()->with('buffer_stack', $buffer_stack);
54 64
    };
55
}
56
57
function endFunc() {
58
    return function(FuncArgs $args) {
59 36
        $buffer_stack = $args->template()->get('buffer_stack') ?: [];
60 36
        if (!count($buffer_stack)) {
61 4
            throw new FuncException('Cannot end a section definition because no section has been started.');
62
        }
63
64 32
        list($ob_level, $callback) = array_pop($buffer_stack);
65
66 32
        if ($ob_level != ob_get_level()) {
67 4
            throw new FuncException('Output buffering level does not match when section was started.');
68
        }
69
70 28
        $contents = ob_get_clean();
71
72 28
        $callback($contents);
73
74 28
        $args->template()->with('buffer_stack', $buffer_stack);
75 56
    };
76
}
77
78
function insertFunc($echo = null) {
79 28
    $echo = $echo ?: Plates\Util\phpEcho();
80
81
    return function(FuncArgs $args) use ($echo) {
82 16
        list($name, $data) = $args->args;
83 16
        $child = $args->template()->fork($name, $data ?: []);
84 16
        $echo($args->render->renderTemplate($child));
85 28
    };
86
}
87
88
function templateDataFunc() {
89
    return function(FuncArgs $args) {
90
        list($data) = $args->args;
91
        return array_merge($args->template()->data, $data);
92 24
    };
93
}
94
95
/** Enables the backwards compatibility with the old extension functions */
96
function wrapSimpleFunc(callable $func, $enable_bc = false) {
97
    return function(FuncArgs $args) use ($func, $enable_bc) {
98
        if ($enable_bc && is_array($func) && isset($func[0]) && $func[0] instanceof Plates\Extension\ExtensionInterface) {
99
            $func[0]->template = $args->template();
0 ignored issues
show
Bug introduced by
Accessing template on the interface League\Plates\Extension\ExtensionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
100
        }
101
102
        return $func(...$args->args);
103
    };
104
}
105
106
function accessTemplatePropFunc($prop) {
107
    return function(FuncArgs $args) use ($prop) {
108
        return $args->template()->{$prop};
109 24
    };
110
}
111
112
function escapeFunc($flags = ENT_COMPAT | ENT_HTML401, $encoding = 'UTF-8') {
113
    return function(FuncArgs $args) use ($flags, $encoding) {
114 4
        return htmlspecialchars($args->args[0], $flags, $encoding);
115 24
    };
116
}
117
118
function assertArgsFunc($num_required, $num_default = 0) {
119
    return function(FuncArgs $args, $next) use ($num_required, $num_default) {
120 16
        if (count($args->args) < $num_required) {
121
            throw new FuncException("Func {$args->func_name} has {$num_required} argument(s).");
122
        }
123
124 16
        if (count($args->args) >= $num_required + $num_default) {
125 4
            return $next($args);
126
        }
127
128 16
        $args = $args->withArgs(array_merge($args->args, array_fill(
129 16
            0,
130 16
            $num_required + $num_default - count($args->args),
131 16
            null
132
        )));
133
134 16
        return $next($args);
135 24
    };
136
}
137
138
function assertTemplateArgsFunc() {
139 24
    return assertArgsFunc(1, 2);
140
}
141
142
/** Creates aliases for certain functions */
143
function aliasNameFunc(array $aliases) {
144
    return function(FuncArgs $args, $next) use ($aliases) {
145 16
        if (!isset($aliases[$args->func_name])) {
146 16
            return $next($args);
147
        }
148
149 4
        while (isset($aliases[$args->func_name])) {
150 4
            $args = $args->withName($aliases[$args->func_name]);
151
        }
152
153 4
        return $next($args);
154 24
    };
155
}
156
157
/** Allows splitting of the handlers from the args name */
158
function splitByNameFunc(array $handlers) {
159
    return function(FuncArgs $args, $next) use ($handlers) {
160 16
        $name = $args->func_name;
161 16
        if (isset($handlers[$name])) {
162 16
            $handler = Plates\Util\stackGroup($handlers[$name]);
163 16
            return $handler($args, $next);
164
        }
165
166
        return $next($args);
167 24
    };
168
}
169
170
function notFoundFunc() {
171
    return function(FuncArgs $args) {
172
        throw new FuncException('The function ' . $args->func_name . ' does not exist.');
173 24
    };
174
}
175