Passed
Push — master ( feae44...7d77cf )
by Josh
03:23
created

Configurator::getAnchors()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 4
nop 0
crap 3
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Autolink;
9
10
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
11
use s9e\TextFormatter\Plugins\ConfiguratorBase;
12
13
class Configurator extends ConfiguratorBase
14
{
15
	/**
16
	* @var string Name of attribute that stores the link's URL
17
	*/
18
	protected $attrName = 'url';
19
20
	/**
21
	* @var bool Whether to match strings that start with "www."
22
	*/
23
	public $matchWww = false;
24
25
	/**
26
	* @var string Name of the tag used to represent links
27
	*/
28
	protected $tagName = 'URL';
29
30
	/**
31
	* Creates the tag used by this plugin
32
	*
33
	* @return void
34
	*/
35 17
	protected function setUp()
36
	{
37 17
		if (isset($this->configurator->tags[$this->tagName]))
38
		{
39 1
			return;
40
		}
41
42
		// Create a tag
43 16
		$tag = $this->configurator->tags->add($this->tagName);
44
45
		// Add an attribute using the default url filter
46 16
		$filter = $this->configurator->attributeFilters->get('#url');
47 16
		$tag->attributes->add($this->attrName)->filterChain->append($filter);
48
49
		// Set the default template
50 16
		$tag->template = '<a href="{@' . $this->attrName . '}"><xsl:apply-templates/></a>';
51
	}
52
53
	/**
54
	* {@inheritdoc}
55
	*/
56 10
	public function asConfig()
57
	{
58
		$config = [
59 10
			'attrName'   => $this->attrName,
60 10
			'regexp'     => $this->getRegexp(),
61 10
			'tagName'    => $this->tagName
62
		];
63 10
		if (!$this->matchWww)
64
		{
65 9
			$config['quickMatch'] = '://';
66
		}
67
68 10
		return $config;
69
	}
70
71
	/**
72
	* Return the regexp used to match URLs
73
	*
74
	* @return strings
0 ignored issues
show
Bug introduced by
The type s9e\TextFormatter\Plugins\Autolink\strings 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...
75
	*/
76 10
	protected function getRegexp()
77
	{
78 10
		$anchor = RegexpBuilder::fromList($this->configurator->urlConfig->getAllowedSchemes()) . '://';
79 10
		if ($this->matchWww)
80
		{
81 1
			$anchor = '(?:' . $anchor . '|www\\.)';
82
		}
83
84 10
		$regexp = '#\\b' . $anchor . '\\S(?>[^\\s()\\[\\]'
85 10
		        . '\\x{FF01}-\\x{FF0F}\\x{FF1A}-\\x{FF20}\\x{FF3B}-\\x{FF40}\\x{FF5B}-\\x{FF65}'
86 10
		        . ']|\\([^\\s()]*\\)|\\[\\w*\\])++#Siu';
87
88 10
		return $regexp;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $regexp returns the type string which is incompatible with the documented return type s9e\TextFormatter\Plugins\Autolink\strings.
Loading history...
89
	}
90
}