Passed
Push — master ( 82b0d0...1ac70f )
by Bobby
09:15
created

Parser::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ballen\Linguist;
4
5
use Ballen\Linguist\Configuration;
6
use Ballen\Linguist\Transformers\PlaintextTransformer;
7
use Ballen\Linguist\Transformers\HtmlTransformer;
8
use Ballen\Linguist\Transformers\MarkdownTansformer;
9
10
/**
11
 * Linguist
12
 *
13
 * Linguist is a PHP library for parsing strings, it can extract and manipulate
14
 *  prefixed words in content ideal for working with @mentions, #topics and
15
 *  even custom action tags!
16
 *
17
 * @author Bobby Allen <[email protected]>
18
 * @license http://www.gnu.org/licenses/gpl-3.0.html
19
 * @link https://github.com/allebb/linguist
20
 * @link http://www.bobbyallen.me
21
 *
22
 */
23
class Parser
24
{
25
26
    /**
27
     * Runtime configuration object storeage.
28
     * @var \Ballen\Linguist\Entities\Configuration
0 ignored issues
show
Bug introduced by
The type Ballen\Linguist\Entities\Configuration 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...
29
     */
30
    private $configuration;
31
32
    /**
33
     * The original message text.
34
     * @var string
35
     */
36
    protected $message;
37
38
    /**
39
     * Class constructor
40
     * @param string $string The string to parse.
41
     * @param \Ballen\Linguist\Configuration $configuration Optional custom tag/url configuration
42
     * @throws InvalidArgumentException
43
     */
44 22
    public function __construct($string, $configuration = null)
45
    {
46 22
        if (is_null($configuration)) {
47 20
            $configuration = new Configuration;
48 20
            $configuration->loadDefault();
49
        }
50 22
        $this->loadConfiguration($configuration);
51 22
        $this->message = $string;
52 22
    }
53
54
    /**
55
     * Sets a custom tag/url configuration.
56
     * @param \Ballen\Linguist\Configuration $configuration
57
     * @return void
58
     */
59 6
    public function setConfiguration(Configuration $configuration)
60
    {
61 6
        $this->loadConfiguration($configuration);
62 6
    }
63
64
    /**
65
     * Return all or a single array of a certain type of tag
66
     * @param string $type The tag name to return
67
     * @return array
68
     * @throws InvalidArgumentException
69
     */
70 12
    public function tags($type = null)
71
    {
72 12
        if ($type === null) {
73 8
            return $this->gatherTags();
74
        }
75 8
        if (isset($this->gatherTags()[$type])) {
76 6
            return $this->gatherTags()[$type];
77
        }
78 2
        throw new \InvalidArgumentException(sprintf('The tag "%s" has no results.', $type));
79
    }
80
81
    /**
82
     * Return the configuration for a specific tag.
83
     * @param string $name The tag name/type.
84
     * @return array
85
     * @throws InvalidArgumentException
86
     */
87 4
    public function tag($name)
88
    {
89 4
        if (isset($this->configuration->get()[$name])) {
90 2
            return $this->configuration->get()[$name];
91
        }
92 2
        throw new \InvalidArgumentException(sprintf('The tag "%s" is not registered!', $name));
93
    }
94
95
    /**
96
     * Generates HTML output by adding HTML links to the tags.
97
     * @return HtmlTransformer
98
     */
99 4
    public function html()
100
    {
101 4
        return new HtmlTransformer($this->plain($this->message), $this->configuration);
0 ignored issues
show
Unused Code introduced by
The call to Ballen\Linguist\Parser::plain() has too many arguments starting with $this->message. ( Ignorable by Annotation )

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

101
        return new HtmlTransformer($this->/** @scrutinizer ignore-call */ plain($this->message), $this->configuration);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
102
    }
103
104
    /**
105
     * Generate Markdown output by adding links to the tags.
106
     * @return MarkdownTansformer
107
     */
108 2
    public function markdown()
109
    {
110 2
        return new MarkdownTansformer($this->plain($this->message), $this->configuration);
0 ignored issues
show
Unused Code introduced by
The call to Ballen\Linguist\Parser::plain() has too many arguments starting with $this->message. ( Ignorable by Annotation )

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

110
        return new MarkdownTansformer($this->/** @scrutinizer ignore-call */ plain($this->message), $this->configuration);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
111
    }
112
113
    /**
114
     * Return the plan text version of the message removing all HTML formatting.
115
     * @return PlaintextTransformer
116
     */
117 18
    public function plain()
118
    {
119 18
        return new PlaintextTransformer($this->message, $this->configuration);
120
    }
121
122
    /**
123
     * Finds, returns and categorises all tags found in the message.
124
     * @return array
125
     */
126 12
    private function gatherTags()
127
    {
128 12
        $tag_configuration = $this->configuration->get();
129 12
        foreach (array_keys($tag_configuration) as $tagtype) {
130 12
            preg_match_all('/\s+' . $tag_configuration[$tagtype]['prefix'] . '(\w+)/', $this->plain($this->message), $matches);
0 ignored issues
show
Unused Code introduced by
The call to Ballen\Linguist\Parser::plain() has too many arguments starting with $this->message. ( Ignorable by Annotation )

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

130
            preg_match_all('/\s+' . $tag_configuration[$tagtype]['prefix'] . '(\w+)/', $this->/** @scrutinizer ignore-call */ plain($this->message), $matches);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
131 12
            $tags[$tagtype] = $matches[1];
132
        }
133 12
        return $tags;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tags seems to be defined by a foreach iteration on line 129. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
134
    }
135
136
    /**
137
     * Loads the configuration into the Parser object.
138
     * @param Configuration $configuration
139
     * @return void
140
     */
141 22
    private function loadConfiguration(Configuration $configuration)
142
    {
143 22
        $this->configuration = $configuration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configuration of type Ballen\Linguist\Configuration is incompatible with the declared type Ballen\Linguist\Entities\Configuration of property $configuration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
144 22
    }
145
146
    /**
147
     * Magic method calls to enable users to call $this->mentions etc.
148
     * @param string $name
149
     * @param array $arguments
150
     * @return array
151
     * @throws RuntimeException
152
     */
153 4
    public function __call($name, $arguments = [])
154
    {
155 4
        $tags = array_keys($this->tags());
156 4
        if (!in_array($name, $tags)) {
157 2
            throw new \RuntimeException('Invalid tag type(s) requested.');
158
        }
159 2
        return $this->tags($name);
160
    }
161
}
162