ANode::loadConfig()   D
last analyzed

Complexity

Conditions 17
Paths 64

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 24
nc 64
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Htsl\Parser\Node\Contracts;
4
5
use Htsl\Htsl;
6
use Htsl\Parser\Document;
7
use Htsl\Helper\TGetter;
8
use Htsl\Helper\IConfigProvider;
9
use Htsl\ReadingBuffer\Line;
10
11
////////////////////////////////////////////////////////////////
12
13
/**
14
 * @property-read string      $nodeType Type of this node.
15
 * @property-read string|null $scope    Whether this node contains a scope and scope name
16
 */
17
abstract class ANode
18
{
19
	use TGetter;
20
21
	/**
22
	 * Htsl main object.
23
	 *
24
	 * @access protected
25
	 *
26
	 * @var \Htsl\Htsl
27
	 */
28
	protected $htsl;
29
30
	/**
31
	 * The document.
32
	 *
33
	 * @access protected
34
	 *
35
	 * @var \Htsl\Parser\Document
36
	 */
37
	protected $document;
38
39
	/**
40
	 * The document.
41
	 *
42
	 * @access protected
43
	 *
44
	 * @var \Htsl\ReadingBuffer\Line
45
	 */
46
	protected $line;
47
48
	/**
49
	 * The config.
50
	 *
51
	 * @access protected
52
	 *
53
	 * @var array
54
	 */
55
	protected $config;
56
57
58
	/**
59
	 * Fake contructor of Node.
60
	 *
61
	 * @access public
62
	 *
63
	 * @param \Htsl\Parser\Document     $document
64
	 * @param \Htsl\ReadingBuffer\Line  $line
65
	 */
66
	final public function __construct( Document$document, Line$line )
67
	{
68
		$this->htsl= $document->htsl;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
		$this->document= $document;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
		$this->line= $line;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
72
		$this->construct();
73
	}
74
75
	/**
76
	 * Real contructor to be rewrite.
77
	 *
78
	 * @access protected
79
	 *
80
	 * @return \Htsl\Parser\Node\Contracts\ANode
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
81
	 */
82
	abstract protected function construct():self;
83
84
	/**
85
	 * Opening this node, and returning node opener.
86
	 *
87
	 * @access public
88
	 *
89
	 * @return string
90
	 */
91
	abstract public function open():string;
92
93
	/**
94
	 * Close this node, and returning node closer.
95
	 *
96
	 * @access public
97
	 *
98
	 * @param  \Htsl\ReadingBuffer\Line   $closerLine  The line when node closed.
99
	 *
100
	 * @return string
101
	 */
102
	abstract public function close( Line$closerLine ):string;
103
104
	/**
105
	 * Getting whether this node contains a scope and scope name.
106
	 *
107
	 * @access public
108
	 *
109
	 * @return string | null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
110
	 */
111
	public function getScope()
112
	{
113
		return null;
114
	}
115
116
	/**
117
	 * Getting the type of this node.
118
	 *
119
	 * @access public
120
	 *
121
	 * @return string
122
	 */
123
	public function getNodeType()
124
	{
125
		static $nodeType;
126
		return $nodeType??$nodeType= $this->nodeType??(static function($className){return strtolower(preg_replace('/(?<=\\w)([A-Z])/','_$1',preg_replace('/^(?:\\w+\\\\)*(\\w+)Node$/','$1',$className)));})(get_class($this));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
127
	}
128
129
	/**
130
	 * Loading node configuration.
131
	 *
132
	 * @access protected
133
	 *
134
	 * @param  string                       $name
135
	 * @param  \Htsl\Helper\IConfigProvider $configProvider
136
	 *
137
	 * @return \Htsl\Parser\Node\Contracts\ANode
138
	 */
139
	protected function loadConfig( string$name, IConfigProvider$configProvider )
140
	{
141
		$config= $configProvider->getConfig($this->nodeType.'_nodes',$name) ?: $configProvider->getConfig($this->nodeType.'_nodes','*');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
142
143
		if( isset($config['multiple']) ){
144
			foreach( $config['multiple'] as $value ){
145
				if( $this->line->pregGet($value['pattern']) ){
146
					$config= $value;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
147
					break;
148
				}
149
			}
150
		}
151
152
		if( isset($config['in']) ){
153
			$config=(function( array$config )use($name){
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
154
				foreach( $config['in'] as $key=>$value ){
155
					if( $this->document->scope && $this->document->scope->scope===$key ){
156
						$value['in_scope']= $key;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
157
						return $value;
158
					}
159
				}
160
				if( !isset($config['out']) ){
161
					$this->document->throw("The $this->nodeType node $name only use in scope ".implode(',',array_keys($config['in'])));
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
162
				}
163
				return $config['out'];
164
			})($config);
165
		}elseif( isset($config['only_in']) && (!$this->document->scope || !in_array($this->document->scope->scope,$config['only_in'])) ){
166
			$this->document->throw("The $this->nodeType node $name only use in scope ".implode(',',$config['only_in']));
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
167
		}elseif( isset($config['not_in']) && (!$this->document->scope || !in_array($this->document->scope->scope,$config['not_in'])) ){
168
			$this->document->throw("The $this->nodeType node $name not use in scope ".implode(',',$config['not_in']));
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
169
		}
170
171
		if( !is_array($config) ){$this->document->throw("The $this->nodeType node $name is not supported.");}
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
172
173
		$this->config= $config;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
174
175
		return $this;
176
	}
177
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
178