bbcodes   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 89
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 8 2
A s9e_format() 0 3 1
A set_data() 0 9 4
A run() 0 11 2
A is_available() 0 3 1
1
<?php
2
/**
3
 *
4
 * Topic Preview
5
 *
6
 * @copyright (c) 2016 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\topicpreview\core\trim\tools;
12
13
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\textformatter\s9e\utils;
0 ignored issues
show
Bug introduced by
The type phpbb\textformatter\s9e\utils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class bbcodes extends base
17
{
18
	/** @var config */
19
	protected $config;
20
21
	/** @var bbcodes_legacy */
22
	protected $trim_bbcodes_legacy;
23
24
	/** @var utils|null */
25
	protected $text_formatter_utils;
26
27
	/** @var array Data array of BBCodes to remove */
28
	protected $data;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param config         $config               Config object
34
	 * @param bbcodes_legacy $trim_bbcodes_legacy  Legacy BBCodes trim tool
35
	 * @param utils|null     $text_formatter_utils Text Formatter Utils
36
	 */
37 38
	public function __construct(config $config, bbcodes_legacy $trim_bbcodes_legacy, utils $text_formatter_utils = null)
38
	{
39 38
		$this->config = $config;
40 38
		$this->trim_bbcodes_legacy = $trim_bbcodes_legacy;
41 38
		$this->text_formatter_utils = $text_formatter_utils;
42 38
	}
43
44
	/**
45
	 * @inheritdoc
46
	 */
47 38
	public function is_available()
48
	{
49 38
		return ($this->text_formatter_utils !== null);
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55 12
	public function run()
56
	{
57
		// If text is not formatted as expected, use legacy bbcode stripper
58 12
		if (!$this->s9e_format())
59 12
		{
60 12
			return $this->trim_bbcodes_legacy
61 12
				->set_text($this->text)
62 12
				->run();
63
		}
64
65 12
		return $this->set_data()->process();
66
	}
67
68
	/**
69
	 * @inheritdoc
70
	 */
71 12
	public function set_data()
72
	{
73 12
		if (!isset($this->data) || !is_array($this->data))
74 12
		{
75 12
			$this->data = !empty($this->config['topic_preview_strip_bbcodes']) ? explode('|', $this->config['topic_preview_strip_bbcodes']) : array();
76 12
			array_unshift($this->data, 'flash');
77 12
		}
78
79 12
		return $this;
80
	}
81
82
	/**
83
	 * Remove specified BBCodes and their contents
84
	 *
85
	 * @return string Stripped message text
86
	 */
87 12
	protected function process()
88
	{
89 12
		foreach ($this->data as $bbcode)
90
		{
91 12
			$this->text = $this->text_formatter_utils->remove_bbcode($this->text, $bbcode);
0 ignored issues
show
Bug introduced by
The method remove_bbcode() does not exist on null. ( Ignorable by Annotation )

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

91
			/** @scrutinizer ignore-call */ 
92
   $this->text = $this->text_formatter_utils->remove_bbcode($this->text, $bbcode);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92 12
		}
93
94 12
		return $this->text_formatter_utils->unparse($this->text);
95
	}
96
97
	/**
98
	 * Is the message s9e formatted
99
	 *
100
	 * @return bool True if message is s9e formatted, false otherwise
101
	 */
102 12
	protected function s9e_format()
103
	{
104 12
		return (bool) preg_match('/^<[rt][ >]/', $this->text);
105
	}
106
}
107