Completed
Push — master ( 27ff23...feae44 )
by Josh
03:17
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\ConfigHelper;
11
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
12
use s9e\TextFormatter\Plugins\ConfiguratorBase;
13
14
class Configurator extends ConfiguratorBase
15
{
16
	/**
17
	* @var string Name of attribute that stores the link's URL
18
	*/
19
	protected $attrName = 'url';
20
21
	/**
22
	* @var bool Whether to match strings that start with "www."
23
	*/
24
	public $matchWww = false;
25
26
	/**
27
	* @var string Name of the tag used to represent links
28
	*/
29
	protected $tagName = 'URL';
30
31
	/**
32
	* Creates the tag used by this plugin
33
	*
34
	* @return void
35
	*/
36 17
	protected function setUp()
37
	{
38 17
		if (isset($this->configurator->tags[$this->tagName]))
39
		{
40 1
			return;
41
		}
42
43
		// Create a tag
44 16
		$tag = $this->configurator->tags->add($this->tagName);
45
46
		// Add an attribute using the default url filter
47 16
		$filter = $this->configurator->attributeFilters->get('#url');
48 16
		$tag->attributes->add($this->attrName)->filterChain->append($filter);
49
50
		// Set the default template
51 16
		$tag->template = '<a href="{@' . $this->attrName . '}"><xsl:apply-templates/></a>';
52
	}
53
54
	/**
55
	* {@inheritdoc}
56
	*/
57 10
	public function asConfig()
58
	{
59
		$config = [
60 10
			'attrName'   => $this->attrName,
61 10
			'regexp'     => $this->getRegexp(),
62 10
			'tagName'    => $this->tagName
63
		];
64 10
		$quickMatch = ConfigHelper::generateQuickMatchFromList($this->getAnchors());
65 10
		if ($quickMatch !== false)
66
		{
67 9
			$config['quickMatch'] = $quickMatch;
68
		}
69
70 10
		return $config;
71
	}
72
73
	/**
74
	* Return the list of possible strings used to start a URL
75
	*
76
	* @return string[]
77
	*/
78 10
	protected function getAnchors()
79
	{
80 10
		$anchors = [];
81 10
		foreach ($this->configurator->urlConfig->getAllowedSchemes() as $scheme)
82
		{
83 10
			$anchors[] = $scheme . '://';
84
		}
85 10
		if ($this->matchWww)
86
		{
87 1
			$anchors[] = 'www.';
88
		}
89
90 10
		return $anchors;
91
	}
92
93
	/**
94
	* Return the regexp used to match URLs
95
	*
96
	* @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...
97
	*/
98 10
	protected function getRegexp()
99
	{
100 10
		$anchor = RegexpBuilder::fromList($this->getAnchors());
101 10
		$regexp = '#\\b' . $anchor . '\\S(?>[^\\s()\\[\\]'
102 10
		        . '\\x{FF01}-\\x{FF0F}\\x{FF1A}-\\x{FF20}\\x{FF3B}-\\x{FF40}\\x{FF5B}-\\x{FF65}'
103 10
		        . ']|\\([^\\s()]*\\)|\\[\\w*\\])++#Siu';
104
105 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...
106
	}
107
}