Completed
Pull Request — development (#2334)
by Joshua
12:46
created

Censor::doCensor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Censor bad words and make them not-so-bad.
5
 * Does the same thing as the old censorText()
6
 *
7
 * @name      ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
10
 *
11
 * This software is a derived product, based on:
12
 *
13
 * Simple Machines Forum (SMF)
14
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
15
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
16
 *
17
 * @version 1.1 dev
18
 *
19
 */
20
21 1
if (!defined('ELK'))
22 1
	die('No access...');
23
24
class Censor
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
25
{
26
	const WHOLE_WORD        = 'censorWholeWord';
27
	const IGNORE_CASE       = 'censorIgnoreCase';
28
	const SHOW_NO_CENSORED  = 'show_no_censored';
29
	const ALLOW_NO_CENSORED = 'allow_no_censored';
30
31
	protected $vulgar = array();
32
	protected $proper = array();
33
	protected $options = array(
34
		self::WHOLE_WORD        => false,
35
		self::IGNORE_CASE       => false,
36
		self::SHOW_NO_CENSORED  => false,
37
		self::ALLOW_NO_CENSORED => false,
38
	);
39
40
	/**
41
	 * Censor constructor.
42
	 * @param array $vulgar
43
	 * @param array $proper
44
	 * @param array $options
45
	 */
46 1
	public function __construct(array $vulgar, array $proper, array $options = array())
47
	{
48 1
		if (count($vulgar) !== count($proper))
49 1
		{
50
			throw new \InvalidArgumentException('Censored vulgar and proper arrays must be equal sizes');
51
		}
52
53 1
		$this->setOptions($options);
54 1
		$this->setVulgarProper($vulgar, $proper);
55 1
	}
56
57
	/**
58
	 * @param array $options
59
	 */
60 1
	protected function setOptions(array $options)
61
	{
62 1
		$this->options = array_merge($this->options, $options);
63 1
	}
64
65
	/**
66
	 * @param array $vulgar
67
	 * @param array $proper
68
	 */
69 1
	protected function setVulgarProper(array $vulgar, array $proper)
70
	{
71
		// Quote them for use in regular expressions.
72 1
		if ($this->options[self::WHOLE_WORD])
73 1
		{
74
			for ($i = 0, $n = count($vulgar); $i < $n; $i++)
75
			{
76
				$vulgar[$i] = str_replace(array('\\\\\\*', '\\*', '&', '\''), array('[*]', '[^\s]*?', '&amp;', '&#039;'), preg_quote($vulgar[$i], '/'));
77
				$vulgar[$i] = '/(?<=^|\W)' . $vulgar[$i] . '(?=$|\W)/u' . (!$this->options[self::IGNORE_CASE] ? '' : 'i');
78
			}
79
		}
80
81 1
		$this->vulgar = $vulgar;
82 1
		$this->proper = $proper;
83 1
	}
84
85
	/**
86
	 * Censor a string
87
	 *
88
	 * @param string $text
89
	 * @param bool $force
90
	 * @return string
91
	 */
92 2
	public function censor($text, $force = false)
93
	{
94 2
		if (empty($this->vulgar) || (!$force && !$this->doCensor()))
95 2
		{
96
			return $text;
97
		}
98
99 2
		if (!$this->options[self::WHOLE_WORD])
100 2
		{
101 2
			$text = !$this->options[self::IGNORE_CASE] ? str_replace($this->vulgar, $this->proper, $text) : str_ireplace($this->vulgar, $this->proper, $text);
102 2
		}
103
		else
104
		{
105
			$text = preg_replace($this->vulgar, $this->proper, $text);
106
		}
107
108 2
		return $text;
109
	}
110
111
	/**
112
	 * Check if we should be censoring.
113
	 *
114
	 * @todo replace with inline checking at some point
115
	 *
116
	 * @return bool
117
	 */
118 2
	public function doCensor()
119
	{
120 2
		global $options, $modSettings;
121
122 2
		return !(!empty($options['show_no_censored']) && !empty($modSettings['allow_no_censored']));
123
	}
124
}