Completed
Push — master ( c71ac0...51eb0e )
by smiley
04:36
created

BBCodeOutputAbstract::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 3
1
<?php
2
/**
3
 * Class BBCodeOutputAbstract
4
 *
5
 * @filesource   BBCodeOutputAbstract.php
6
 * @created      23.04.2018
7
 * @package      chillerlan\BBCode\Output
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\BBCode\Output;
14
15
use chillerlan\Traits\{
16
	ClassLoader, ContainerInterface
17
};
18
use Psr\Log\LoggerInterface;
19
use Psr\SimpleCache\CacheInterface;
20
21
abstract class BBCodeOutputAbstract implements BBCodeOutputInterface{
22
	use ClassLoader;
23
24
	/**
25
	 * @var string[]
26
	 */
27
	protected $modules = [];
28
29
	/**
30
	 * @var string[]
31
	 */
32
	protected $tagmap = [];
33
34
	/**
35
	 * Holds an array of singletags
36
	 *
37
	 * @var string[]
38
	 */
39
	protected $singletags = [];
40
41
	/**
42
	 * Holds an array of noparse tags
43
	 *
44
	 * @var string[]
45
	 */
46
	protected $noparse = [];
47
48
	/**
49
	 * @var \chillerlan\BBCode\BBCodeOptions
50
	 */
51
	protected $options;
52
53
	/**
54
	 * @var \Psr\SimpleCache\CacheInterface
55
	 */
56
	protected $cache;
57
58
	/**
59
	 * @var \Psr\Log\LoggerInterface
60
	 */
61
	protected $logger;
62
63
	/**
64
	 * @var \chillerlan\BBCode\Output\BBCodeModuleInterface[]
65
	 */
66
	protected $moduleInterfaces = [];
67
68
	/**
69
	 * BBCodeOutputInterface constructor.
70
	 *
71
	 * @param \chillerlan\Traits\ContainerInterface $options
72
	 * @param \Psr\SimpleCache\CacheInterface  $cache
73
	 * @param \Psr\Log\LoggerInterface         $logger
74
	 */
75
	public function __construct(ContainerInterface $options, CacheInterface $cache, LoggerInterface $logger){
76
		$this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
$options is of type object<chillerlan\Traits\ContainerInterface>, but the property $options was declared to be of type object<chillerlan\BBCode\BBCodeOptions>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
77
		$this->cache   = $cache;
78
		$this->logger  = $logger;
79
80
		foreach($this->modules as $module){
81
			/** @var \chillerlan\BBCode\Output\BBCodeModuleInterface $moduleInterface */
82
			$moduleInterface = $this->loadClass($module, BBCodeModuleInterface::class, $this->options, $this->cache, $this->logger);
83
			foreach($moduleInterface->getTags() as $tag){
84
				$this->tagmap[$tag] = $module;
85
			}
86
87
			$this->noparse    = array_merge($this->noparse, $moduleInterface->getNoparse());
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->nopar...nterface->getNoparse()) of type array is incompatible with the declared type array<integer,string> of property $noparse.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
			$this->singletags = array_merge($this->singletags, $moduleInterface->getSingleTags());
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->singl...rface->getSingleTags()) of type array is incompatible with the declared type array<integer,string> of property $singletags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
90
			$this->moduleInterfaces[$module] = $moduleInterface;
91
		}
92
93
	}
94
95
	/**
96
	 * @inheritdoc
97
	 */
98
	public function getTags():array {
99
		return array_keys($this->tagmap);
100
	}
101
102
	/**
103
	 * @inheritdoc
104
	 */
105
	public function getSingleTags():array {
106
		return $this->singletags;
107
	}
108
109
	/**
110
	 * @inheritdoc
111
	 */
112
	public function getNoparse():array{
113
		return $this->noparse;
114
	}
115
116
	/**
117
	 * @inheritdoc
118
	 */
119
	public function transform(string $tag, array $attributes, string $content, string $match, int $callback_count):string{
120
121
		if(!in_array($tag, array_keys($this->tagmap), true)){
122
			return $match; // $content
123
		}
124
125
		return call_user_func_array([$this->moduleInterfaces[$this->tagmap[$tag]], $tag], [$tag, $attributes, $content, $match, $callback_count]);
126
	}
127
128
}
129