Macros   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A get() 0 8 2
A set() 0 5 1
A text() 0 9 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Core
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Core".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Utility;
17
18
use JBZoo\Utils\Arr;
19
use Cake\ORM\Entity;
20
use Cake\Utility\Hash;
21
use Cake\Routing\Router;
22
23
/**
24
 * Class Macros
25
 *
26
 * @package Core\Utility
27
 */
28
class Macros
29
{
30
31
    /**
32
     * Replacement data list.
33
     *
34
     * @var array|Entity
35
     */
36
    protected $_data = [];
37
38
    /**
39
     * Macros constructor.
40
     *
41
     * @param   mixed $data
42
     */
43
    public function __construct($data = [])
44
    {
45
        if ($data instanceof Entity) {
46
            $data = $data->toArray();
47
        }
48
49
        $this->_data = (array) $data;
50
51
        $this->set('base_url', Router::fullBaseUrl());
52
    }
53
54
    /**
55
     * Get replacement val or all list.
56
     *
57
     * @param   null|string|int $key
58
     * @return  array
59
     */
60
    public function get($key = null)
61
    {
62
        if (Arr::key($key, $this->_data)) {
63
            return $this->_data[$key];
64
        }
65
66
        return $this->_data;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_data; of type array|Cake\ORM\Entity adds the type Cake\ORM\Entity to the return on line 66 which is incompatible with the return type documented by Core\Utility\Macros::get of type array.
Loading history...
67
    }
68
69
    /**
70
     * Add new value in list.
71
     *
72
     * @param   string|int $key
73
     * @param   string|int $val
74
     * @return  $this
75
     */
76
    public function set($key, $val)
77
    {
78
        $this->_data = Hash::merge([$key => $val], $this->_data);
79
        return $this;
80
    }
81
82
    /**
83
     * Get replacement text.
84
     *
85
     * @param   string $text
86
     * @return  string mixed
87
     */
88
    public function text($text)
89
    {
90
        foreach ($this->_data as $macros => $value) {
0 ignored issues
show
Bug introduced by
The expression $this->_data of type array|object<Cake\ORM\Entity> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
91
            $macros = '{' . $macros . '}';
92
            $text   = preg_replace('#' . $macros . '#ius', $value, $text);
93
        }
94
95
        return $text;
96
    }
97
}
98