BBCode   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 43
lcom 1
cbo 9
dl 0
loc 295
rs 8.96
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A allowTags() 0 13 3
A setCache() 0 5 1
C setOptions() 0 55 17
B parse() 0 40 5
B parseBBCode() 0 50 10
B parseAttributes() 0 27 6

How to fix   Complexity   

Complex Class

Complex classes like BBCode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BBCode, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Class BBCode
4
 *
5
 * @filesource   BBCode.php
6
 * @created      19.04.2018
7
 * @package      chillerlan\BBCode
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\BBCode;
14
15
use chillerlan\BBCode\Output\BBCodeOutputInterface;
16
use chillerlan\Settings\SettingsContainerInterface;
17
use Psr\Log\{
18
	LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger
19
};
20
use Psr\SimpleCache\CacheInterface;
21
22
class BBCode implements LoggerAwareInterface{
23
	use LoggerAwareTrait;
24
25
	/**
26
	 * @var \chillerlan\BBCode\BBCodeOptions|\chillerlan\Settings\SettingsContainerInterface
27
	 */
28
	protected $options;
29
30
	/**
31
	 * @var \Psr\SimpleCache\CacheInterface|\chillerlan\BBCode\BBCache
32
	 */
33
	protected $cache;
34
35
	/**
36
	 * @var \chillerlan\BBCode\SanitizerInterface
37
	 */
38
	protected $sanitizerInterface;
39
40
	/**
41
	 * @var \chillerlan\BBCode\Output\BBCodeOutputInterface
42
	 */
43
	protected $outputInterface;
44
45
	/**
46
	 * @var \chillerlan\BBCode\ParserMiddlewareInterface
47
	 */
48
	protected $parserMiddleware;
49
50
	/**
51
	 * @var array
52
	 */
53
	protected $tags = [];
54
55
	/**
56
	 * @var array
57
	 */
58
	protected $noparse = [];
59
60
	/**
61
	 * @var array
62
	 */
63
	protected $allowed = [];
64
65
	/**
66
	 * @var int
67
	 */
68
	protected $limit;
69
70
	/**
71
	 * BBCode constructor.
72
	 *
73
	 * @param \chillerlan\Settings\SettingsContainerInterface|null $options
74
	 * @param \Psr\SimpleCache\CacheInterface|null                 $cache
75
	 * @param \Psr\Log\LoggerInterface|null                        $logger
76
	 */
77
	public function __construct(SettingsContainerInterface $options = null, CacheInterface $cache = null, LoggerInterface $logger = null){
78
		$this
79
			->setCache($cache ?? new BBCache)
80
			->setLogger($logger ?? new NullLogger);
81
82
		$this->setOptions($options ?? new BBCodeOptions);
83
	}
84
85
	/**
86
	 * @param array $allowedTags
87
	 *
88
	 * @return \chillerlan\BBCode\BBCode
89
	 */
90
	public function allowTags(array $allowedTags):BBCode{
91
		$this->allowed = [];
92
93
		foreach($allowedTags as $tag){
94
			$tag = strtolower($tag);
95
96
			if(in_array($tag, $this->tags, true)){
97
				$this->allowed[] = $tag;
98
			}
99
		}
100
101
		return $this;
102
	}
103
104
	/**
105
	 * @param \Psr\SimpleCache\CacheInterface $cache
106
	 *
107
	 * @return \chillerlan\BBCode\BBCode
108
	 */
109
	public function setCache(CacheInterface $cache):BBCode{
110
		$this->cache = $cache;
111
112
		return $this;
113
	}
114
115
	/**
116
	 * @todo
117
	 *
118
	 * @param \chillerlan\Settings\SettingsContainerInterface $options
119
	 *
120
	 * @throws \chillerlan\BBCode\BBCodeException
121
	 * @return \chillerlan\BBCode\BBCode
122
	 */
123
	public function setOptions(SettingsContainerInterface $options):BBCode{
124
		$this->options = $options;
125
126
		mb_internal_encoding('UTF-8');
127
128
		if(
129
			ini_set('pcre.backtrack_limit', $this->options->pcre_backtrack_limit) === false
0 ignored issues
show
Bug introduced by
Accessing pcre_backtrack_limit on the interface chillerlan\Settings\SettingsContainerInterface 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...
130
			|| ini_set('pcre.recursion_limit', $this->options->pcre_recursion_limit) === false
0 ignored issues
show
Bug introduced by
Accessing pcre_recursion_limit on the interface chillerlan\Settings\SettingsContainerInterface 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...
131
			|| ini_set('pcre.jit', $this->options->pcre_jit) === false
0 ignored issues
show
Bug introduced by
Accessing pcre_jit on the interface chillerlan\Settings\SettingsContainerInterface 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...
132
		){
133
			throw new BBCodeException('could not alter ini settings');
134
		}
135
136
		if(ini_get('pcre.backtrack_limit') !== (string)$this->options->pcre_backtrack_limit
0 ignored issues
show
Bug introduced by
Accessing pcre_backtrack_limit on the interface chillerlan\Settings\SettingsContainerInterface 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...
137
			|| ini_get('pcre.recursion_limit') !== (string)$this->options->pcre_recursion_limit
0 ignored issues
show
Bug introduced by
Accessing pcre_recursion_limit on the interface chillerlan\Settings\SettingsContainerInterface 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...
138
			|| ini_get('pcre.jit') !== (string)$this->options->pcre_jit
0 ignored issues
show
Bug introduced by
Accessing pcre_jit on the interface chillerlan\Settings\SettingsContainerInterface 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...
139
		){
140
			throw new BBCodeException('ini settings differ from options');
141
		}
142
143
		if($this->options->sanitizeInput || $this->options->sanitizeOutput){
0 ignored issues
show
Bug introduced by
Accessing sanitizeInput on the interface chillerlan\Settings\SettingsContainerInterface 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...
Bug introduced by
Accessing sanitizeOutput on the interface chillerlan\Settings\SettingsContainerInterface 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...
144
			$this->sanitizerInterface  = new $this->options->sanitizerInterface($this->options);
0 ignored issues
show
Bug introduced by
Accessing sanitizerInterface on the interface chillerlan\Settings\SettingsContainerInterface 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...
145
146
			if(!$this->sanitizerInterface instanceof SanitizerInterface){
147
				throw new BBcodeException('invalid SanitizerInterface');
148
			}
149
		}
150
151
		if($this->options->preParse || $this->options->postParse){
0 ignored issues
show
Bug introduced by
Accessing preParse on the interface chillerlan\Settings\SettingsContainerInterface 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...
Bug introduced by
Accessing postParse on the interface chillerlan\Settings\SettingsContainerInterface 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...
152
			$this->parserMiddleware = new $this->options->parserMiddlewareInterface($this->options, $this->cache, $this->logger);
0 ignored issues
show
Bug introduced by
Accessing parserMiddlewareInterface on the interface chillerlan\Settings\SettingsContainerInterface 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...
153
154
			if(!$this->parserMiddleware instanceof ParserMiddlewareInterface){
155
				throw new BBcodeException('invalid ParserMiddlewareInterface');
156
			}
157
		}
158
159
		$this->outputInterface = new $this->options->outputInterface($this->options, $this->cache, $this->logger);
0 ignored issues
show
Bug introduced by
Accessing outputInterface on the interface chillerlan\Settings\SettingsContainerInterface 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...
160
161
		if(!$this->outputInterface instanceof BBCodeOutputInterface){
162
			throw new BBcodeException('invalid BBCodeOutputInterface');
163
		}
164
165
		$this->tags    = $this->outputInterface->getTags();
166
		$this->noparse = $this->outputInterface->getNoparse();
167
		$this->limit   = (int)$this->options->nestingLimit;
0 ignored issues
show
Bug introduced by
Accessing nestingLimit on the interface chillerlan\Settings\SettingsContainerInterface 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...
168
169
		if(is_array($this->options->allowedTags) && !empty($this->options->allowedTags)){
0 ignored issues
show
Bug introduced by
Accessing allowedTags on the interface chillerlan\Settings\SettingsContainerInterface 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...
170
			$this->allowTags($this->options->allowedTags);
0 ignored issues
show
Bug introduced by
Accessing allowedTags on the interface chillerlan\Settings\SettingsContainerInterface 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...
171
		}
172
		elseif($this->options->allowAvailableTags === true){
0 ignored issues
show
Bug introduced by
Accessing allowAvailableTags on the interface chillerlan\Settings\SettingsContainerInterface 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...
173
			$this->allowed = $this->tags;
174
		}
175
176
		return $this;
177
	}
178
179
	/**
180
	 * Transforms a BBCode string to HTML (or whatevs)
181
	 *
182
	 * @param string $bbcode
183
	 *
184
	 * @return string
185
	 */
186
	public function parse(string $bbcode):string{
187
188
		// sanitize the input if needed
189
		if($this->options->sanitizeInput){
0 ignored issues
show
Bug introduced by
Accessing sanitizeInput on the interface chillerlan\Settings\SettingsContainerInterface 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...
190
			$bbcode = $this->sanitizerInterface->sanitizeInput($bbcode);
191
		}
192
193
		// run the pre-parser
194
		if($this->options->preParse){
0 ignored issues
show
Bug introduced by
Accessing preParse on the interface chillerlan\Settings\SettingsContainerInterface 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...
195
			$bbcode = $this->parserMiddleware->pre($bbcode);
196
		}
197
198
		// @todo: array < 2 elements causes a PREG_BACKTRACK_LIMIT_ERROR! (breaks match pattern)
199
		$singleTags = array_merge(['br', 'hr'], $this->outputInterface->getSingleTags());
200
201
		// close singletags: [br] -> [br][/br]
202
		$bbcode = preg_replace('#\[('.implode('|', $singleTags).')((?:\s|=)[^]]*)?]#is', '[$1$2][/$1]', $bbcode);
203
204
		// @todo: find non-singletags without a closing tag and close them (or convert the brackets to entities)
205
206
		// protect newlines
207
		$bbcode = str_replace(["\r", "\n"], ['', $this->options->placeholder_eol], $bbcode);
0 ignored issues
show
Bug introduced by
Accessing placeholder_eol on the interface chillerlan\Settings\SettingsContainerInterface 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...
208
		// parse the bbcode
209
		$bbcode = $this->parseBBCode($bbcode);
210
211
		// run the post-parser
212
		if($this->options->postParse){
0 ignored issues
show
Bug introduced by
Accessing postParse on the interface chillerlan\Settings\SettingsContainerInterface 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...
213
			$bbcode = $this->parserMiddleware->post($bbcode);
214
		}
215
216
		// replace the newline placeholders
217
		$bbcode = str_replace($this->options->placeholder_eol, $this->outputInterface->getEOL(), $bbcode);
0 ignored issues
show
Bug introduced by
Accessing placeholder_eol on the interface chillerlan\Settings\SettingsContainerInterface 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...
218
219
		// run the sanitizer/html purifier/whatever as a final step
220
		if($this->options->sanitizeOutput){
0 ignored issues
show
Bug introduced by
Accessing sanitizeOutput on the interface chillerlan\Settings\SettingsContainerInterface 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...
221
			$bbcode = $this->sanitizerInterface->sanitizeOutput($bbcode);
222
		}
223
224
		return $bbcode;
225
	}
226
227
	/**
228
	 * @param $bbcode
229
	 *
230
	 * @return string
231
	 */
232
	protected function parseBBCode($bbcode):string{
233
		static $callback_count = 0;
234
235
		$callback = false;
236
237
		if(is_array($bbcode) && count($bbcode) === 4){
238
			[$match, $tag, $attributes, $content] = $bbcode;
0 ignored issues
show
Bug introduced by
The variable $match seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $tag seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $attributes seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $content seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
239
240
			$tag        = strtolower($tag);
0 ignored issues
show
Bug introduced by
The variable $tag seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
241
			$attributes = $this->parseAttributes($attributes);
0 ignored issues
show
Bug introduced by
The variable $attributes seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
242
			$callback   = true;
243
244
			$callback_count++;
245
		}
246
		else if(is_string($bbcode) && !empty($bbcode)){
247
			$match      = null;
248
			$tag        = null;
249
			$attributes = [];
250
			$content    = $bbcode;
251
		}
252
		else{
253
			return '';
254
		}
255
256
		if($callback_count < $this->limit && !in_array($tag, $this->noparse , true)){
257
			$content = preg_replace_callback('#\[(\w+)((?:\s|=)[^]]*)?]((?:[^[]|\[(?!/?\1((?:\s|=)[^]]*)?])|(?R))*)\[/\1]#', __METHOD__, $content);
0 ignored issues
show
Bug introduced by
The variable $content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
258
			$e = preg_last_error();
259
260
			/**
261
			 * 1 - PREG_INTERNAL_ERROR
262
			 * 2 - PREG_BACKTRACK_LIMIT_ERROR
263
			 * 3 - PREG_RECURSION_LIMIT_ERROR
264
			 * 4 - PREG_BAD_UTF8_ERROR
265
			 * 5 - PREG_BAD_UTF8_OFFSET_ERROR
266
			 * 6 - PREG_JIT_STACKLIMIT_ERROR
267
			 */
268
			if($e !== PREG_NO_ERROR){
269
				$this->logger->debug('preg_error', ['errno' => $e, '$content' => $content]);
270
271
				$content = $match ?? '';//$content ?? $bbcode ??
272
			}
273
		}
274
275
		if($callback === true && in_array($tag, $this->allowed, true)){
276
			$content = $this->outputInterface->transform($tag, $attributes, $content, $match, $callback_count);
0 ignored issues
show
Bug introduced by
The variable $match does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
277
			$callback_count = 0;
278
		}
279
280
		return $content;
281
	}
282
283
	/**
284
	 * @param string $attributes
285
	 *
286
	 * @return array
287
	 */
288
	protected function parseAttributes(string $attributes):array{
289
		$attr = [];
290
291
		if(empty($attributes)){
292
			return $attr;
293
		}
294
295
		// @todo: fix attributes pattern: accept single and double quotes around the value
296
		if(preg_match_all('#(?<name>^|[[a-z]+)\=(["\']?)(?<value>[^"\']*?)\2(?: |$)#i', $attributes, $matches, PREG_SET_ORDER) > 0){
297
#			print_r(['$attributes' => $attributes, '$matches' => $matches]);
298
299
			foreach($matches as $attribute){
300
				$name = empty($attribute['name']) ? $this->options->placeholder_bbtag : strtolower(trim($attribute['name']));
0 ignored issues
show
Bug introduced by
Accessing placeholder_bbtag on the interface chillerlan\Settings\SettingsContainerInterface 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...
301
302
				$attr[$name] = trim($attribute['value'], '"\' ');
303
			}
304
		}
305
306
		$e = preg_last_error();
307
308
		if($e !== PREG_NO_ERROR){
309
			$this->logger->debug('preg_error', ['errno' => $e, '$attributes' => $attributes]);
310
			$attr['__error__'] = $attributes;
311
		}
312
313
		return $attr;
314
	}
315
316
}
317