Completed
Push — master ( ea2170...fb436f )
by Matt
07:09
created

bbcodes   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 91
ccs 28
cts 28
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A is_available() 0 4 1
A run() 0 12 2
A set_data() 0 10 4
A process() 0 9 2
A s9e_format() 0 4 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;
14
use phpbb\textformatter\s9e\utils;
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();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface vse\topicpreview\core\trim\tools\tool_interface as the method process() does only exist in the following implementations of said interface: vse\topicpreview\core\trim\tools\bbcodes, vse\topicpreview\core\trim\tools\bbcodes_legacy, vse\topicpreview\core\trim\tools\markup.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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);
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][ >]/s', $this->text);
105
	}
106
}
107