ControlNode::getScope()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Htsl\Parser\Node;
4
5
use Htsl\Htsl;
6
use Htsl\ReadingBuffer\Line;
7
use Htsl\Parser\Node\Contracts\ANode;
8
9
////////////////////////////////////////////////////////////////
10
11
class ControlNode extends ANode
12
{
13
	/**
14
	 * The name of the Htsl.php control structure.
15
	 *
16
	 * @access private
17
	 *
18
	 * @var string
19
	 */
20
	private $name;
21
22
	/**
23
	 * The name of the complied(PHP) control structure.
24
	 *
25
	 * @access private
26
	 *
27
	 * @var string
28
	 */
29
	private $structureName;
30
31
	/**
32
	 * Parameters.
33
	 *
34
	 * @access private
35
	 *
36
	 * @var string
37
	 */
38
	private $param;
39
40
	/**
41
	 * Unique id for check whether loop executed.
42
	 *
43
	 * @access private
44
	 *
45
	 * @var string
46
	 */
47
	private $id;
48
49
	/**
50
	 * Real constructor.
51
	 *
52
	 * @access protected
53
	 *
54
	 * @return \Htsl\Parser\Node\Contracts\ANode
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ControlNode.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
55
	 */
56
	protected function construct():parent
57
	{
58
		$name= $this->line->pregGet('/(?<=^~)[\w-]*/');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
59
		$this->name=$name;
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...
60
61
		$this->loadConfig($name,$this->htsl);
62
63
		$this->param= $this->line->pregGet('/^~[\w-]*\( (.*) \)/',1);
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...
64
65
		$this->structureName=$this->config['name']??$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...
66
67
		$this->id=strtoupper(uniqid());
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...
68
69
		return $this;
70
	}
71
72
	/**
73
	 * Opening this control node, and returning node opener.
74
	 *
75
	 * @access public
76
	 *
77
	 * @return string
78
	 */
79
	public function open():string
80
	{
81
		return $this->withParam($this->config['opener']);
82
	}
83
84
	/**
85
	 * Getting whether this node contains a scope and scope name.
86
	 *
87
	 * @access public
88
	 *
89
	 * @return string | null
90
	 */
91
	public function getScope()
92
	{
93
		return $this->config['scope']??null;
94
	}
95
96
97
	/**
98
	 * Close this control node, and returning node closer.
99
	 *
100
	 * @access public
101
	 *
102
	 * @param  \Htsl\ReadingBuffer\Line   $closerLine  The line when node closed.
103
	 *
104
	 * @return string
105
	 */
106
	public function close( Line$closerLine ):string
107
	{
108
		if( isset($this->config['close_by']) && $closerLine->indentLevel==$this->line->indentLevel ){
109
			foreach( $this->config['close_by'] as $key=>$value ){
110
				if( $closerLine->pregMatch($key) ){
111
					return $this->withParam($value);
112
				}
113
			}
114
		}
115
116
		if( isset($this->config['closer']) )
117
			{ return $this->withParam($this->config['closer']); }
118
119
		return '';
120
	}
121
122
	/**
123
	 * Parse opener or closer with parameters.
124
	 *
125
	 * @access private
126
	 *
127
	 * @param  string $input Opener or Closer
128
	 *
129
	 * @return string
130
	 */
131
	private function withParam( string$input )
132
	{
133
		return str_replace('$_FLAG_$',"__HTSL_CTRL_FLAG_{$this->id}__",preg_replace_callback('/(?<!%)%s((?:\\/.+?(?<!\\\\)\\/.+?(?<!\\\\)\\/)+)?/',function( array$matches ){
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...
134
			$param= $this->param;
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...
135
136
			if( isset($matches[1]) ){
137
				array_map(...[
138
					function($replacer)use(&$param){
139
						list($pattern,$replacement,)= preg_split('/(?<!\\\\)\\//',$replacer);
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...
140
						$param= preg_replace(...[
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 23 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...
141
							"/$pattern/",
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $pattern 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...
142
							preg_replace('/^\\\\_$/','',$replacement),
143
							$param,
144
						]);
145
					},
146
					preg_split(
147
						'/(?<!\\\\)\\/\\//'
148
						,
149
						trim($matches[1],'/')
150
					),
151
				]);
152
			}
153
			return $param;
154
		},$input));
155
	}
156
}
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...
157