Completed
Push — master ( 7bbe0a...7cfc8f )
by Matt
02:33
created

trim_tools::remove_bbcode_contents()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
/**
3
*
4
* Topic Preview
5
*
6
* @copyright (c) 2014 Matt Friedman
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace vse\topicpreview\core;
12
13
class trim_tools
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\textformatter\s9e\utils */
19
	protected $text_formatter_utils;
20
21
	/** @var string|array BBcodes to be removed */
22
	protected $strip_bbcodes;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\config\config $config
28
	 * @param \phpbb\textformatter\s9e\utils $text_formatter_utils
0 ignored issues
show
Documentation introduced by
Should the type for parameter $text_formatter_utils not be null|\phpbb\textformatter\s9e\utils?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
29
	 * @access public
30
	 */
31
	public function __construct(\phpbb\config\config $config, \phpbb\textformatter\s9e\utils $text_formatter_utils = null)
32
	{
33
		$this->config = $config;
34
		$this->text_formatter_utils = $text_formatter_utils;
35
	}
36
37
	/**
38
	 * Trim and clean text
39
	 *
40
	 * @param string $message Message text
41
	 * @param int    $length  The length to trim text to
42
	 * @return string Trimmed message text
43
	 * @access protected
44
	 */
45
	public function trim_text($message, $length)
46
	{
47
		$message = $this->remove_markup($message);
48
49
		if (utf8_strlen($message) <= $length)
50
		{
51
			return $this->tp_nl2br($message);
52
		}
53
54
		// trim the text to the last whitespace character before the cut-off
55
		$message = preg_replace('/\s+?(\S+)?$/', '', utf8_substr($message, 0, $length));
56
57
		return $this->tp_nl2br($message) . '...';
58
	}
59
60
	/**
61
	 * Strip BBCodes, tags and links from text
62
	 *
63
	 * @param string $message Message text
64
	 * @return string Cleaned message text
65
	 * @access protected
66
	 */
67
	protected function remove_markup($message)
68
	{
69
		$message = smiley_text($message, true); // display smileys as text :)
70
71
		$message = $this->remove_bbcode_contents($message);
72
73
		static $patterns = array();
74
75
		if (empty($patterns))
76
		{
77
			// RegEx patterns based on Topic Text Hover Mod by RMcGirr83
78
			$patterns = array(
79
				'#<!-- [lmw] --><a class="postlink[^>]*>(.*<\/a[^>]*>)?<!-- [lmw] -->#Usi', // Magic URLs
80
				'#<[^>]*>(.*<[^>]*>)?#Usi', // HTML code
81
				'#\[/?[^\[\]]+\]#mi', // All BBCode tags
82
				'#(http|https|ftp|mailto)(:|\&\#58;)\/\/[^\s]+#i', // Remaining URLs
83
				'#"#', // Possible un-encoded quotes from older board conversions
84
				'#[ \t]{2,}#' // Multiple spaces #[\s]+#
85
			);
86
		}
87
88
		return trim(preg_replace($patterns, ' ', $message));
89
	}
90
91
	/**
92
	 * Strip special BBCodes and their contents
93
	 *
94
	 * @param string $message Message text
95
	 * @return string Stripped message text
96
	 * @access protected
97
	 */
98
	protected function remove_bbcode_contents($message)
99
	{
100
		if ($this->text_formatter_utils == null)
101
		{
102
			return $this->strip_bbcode_contents($message);
103
		}
104
105
		if (!isset($this->strip_bbcodes))
106
		{
107
			$this->strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? explode('|', $this->config['topic_preview_strip_bbcodes']) : array();
108
			array_unshift($this->strip_bbcodes, 'flash');
109
		}
110
111
		foreach ($this->strip_bbcodes as $bbcode)
0 ignored issues
show
Bug introduced by
The expression $this->strip_bbcodes of type string|array 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...
112
		{
113
			$message = $this->text_formatter_utils->remove_bbcode($message, $bbcode);
114
		}
115
116
		return $message;
117
	}
118
119
	/**
120
	 * Strip special BBCodes and their contents
121
	 * Uses recursion to handle nested BBCodes
122
	 *
123
	 * @param string $message Message text
124
	 * @return string Stripped message text
125
	 * @access protected
126
	 */
127
	protected function strip_bbcode_contents($message)
128
	{
129
		if (!isset($this->strip_bbcodes))
130
		{
131
			$strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? 'flash|' . trim($this->config['topic_preview_strip_bbcodes']) : 'flash';
132
			$this->strip_bbcodes = '#\[(' . $strip_bbcodes . ')[^\[\]]*\]((?:(?!\[\1[^\[\]]*\]).)*)\[\/\1[^\[\]]*\]#Usi';
133
		}
134
135
		if (preg_match($this->strip_bbcodes, $message))
136
		{
137
			return $this->strip_bbcode_contents(preg_replace($this->strip_bbcodes, '', $message));
138
		}
139
140
		return $message;
141
	}
142
143
	/**
144
	 * Convert and preserve line breaks
145
	 *
146
	 * @param string $message Message text
147
	 * @return string Message text with line breaks
148
	 * @access protected
149
	 */
150
	protected function tp_nl2br($message)
151
	{
152
		// http://stackoverflow.com/questions/816085/removing-redundant-line-breaks-with-regular-expressions
153
		return nl2br(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $message));
154
	}
155
}
156