Passed
Push — master ( 417605...635760 )
by Divine Niiquaye
11:55
created

SlotsHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 21.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 105
ccs 6
cts 28
cp 0.2143
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 15 3
A stop() 0 9 2
A set() 0 3 1
A has() 0 3 1
A getName() 0 3 1
A get() 0 3 1
A start() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Helper;
19
20
/**
21
 * SlotsHelper manages template slots.
22
 *
23
 * @author Fabien Potencier <[email protected]>
24
 * @author Divine Niiquaye Ibok <[email protected]>
25
 */
26
class SlotsHelper extends AbstractHelper
27
{
28
    /** @var array<string,mixed> */
29
    protected $slots = [];
30
31
    /** @var array<int,string> */
32
    protected $openSlots = [];
33
34
    /**
35
     * Starts a new slot.
36
     *
37
     * This method starts an output buffer that will be
38
     * closed when the stop() method is called.
39
     *
40
     * @throws \InvalidArgumentException if a slot with the same name is already started
41
     */
42
    public function start(string $name): void
43
    {
44
        if (\in_array($name, $this->openSlots, true)) {
45
            throw new \InvalidArgumentException(\sprintf('A slot named "%s" is already started.', $name));
46
        }
47
48
        $this->openSlots[] = $name;
49
        $this->slots[$name] = '';
50
51
        \ob_start();
52
        \ob_implicit_flush(\PHP_VERSION_ID >= 80000 ? false : 0);
0 ignored issues
show
Bug introduced by
It seems like PHP_VERSION_ID >= 80000 ? false : 0 can also be of type integer; however, parameter $enable of ob_implicit_flush() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        \ob_implicit_flush(/** @scrutinizer ignore-type */ \PHP_VERSION_ID >= 80000 ? false : 0);
Loading history...
53
    }
54
55
    /**
56
     * Stops a slot.
57
     *
58
     * @throws \LogicException if no slot has been started
59
     */
60
    public function stop(): void
61
    {
62
        if (!$this->openSlots) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->openSlots of type array<integer,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
            throw new \LogicException('No slot started.');
64
        }
65
66
        $name = \array_pop($this->openSlots);
67
68
        $this->slots[$name] = \ob_get_clean();
69
    }
70
71
    /**
72
     * Returns true if the slot exists.
73
     *
74
     * @return bool
75
     */
76
    public function has(string $name)
77
    {
78
        return isset($this->slots[$name]);
79
    }
80
81
    /**
82
     * Gets the slot value.
83
     *
84
     * @param bool|string $default The default slot content
85
     *
86
     * @return string|false The slot content
87
     */
88 5
    public function get(string $name, $default = false)
89
    {
90 5
        return $this->slots[$name] ?? $default;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->slots[$name] ?? $default also could return the type boolean which is incompatible with the documented return type false|string.
Loading history...
91
    }
92
93
    /**
94
     * Sets a slot value.
95
     */
96 5
    public function set(string $name, string $content): void
97
    {
98 5
        $this->slots[$name] = $content;
99
    }
100
101
    /**
102
     * Outputs a slot.
103
     *
104
     * @param bool|string $default The default slot content
105
     *
106
     * @return bool true if the slot is defined or if a default content has been provided, false otherwise
107
     */
108
    public function output(string $name, $default = false): bool
109
    {
110
        if (!isset($this->slots[$name])) {
111
            if (false !== $default) {
112
                echo $default;
0 ignored issues
show
Bug introduced by
Are you sure $default of type string|true can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
                echo /** @scrutinizer ignore-type */ $default;
Loading history...
113
114
                return true;
115
            }
116
117
            return false;
118
        }
119
120
        echo $this->slots[$name];
121
122
        return true;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 6
    public function getName(): string
129
    {
130 6
        return 'slots';
131
    }
132
}
133