Skip to content

Commit cf0bb68

Browse files
committed
Small refactor - Added private methods for DTO
1 parent 382b98c commit cf0bb68

File tree

10 files changed

+95
-60
lines changed

10 files changed

+95
-60
lines changed

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/tests export-ignore
2+
/tests linguist-documentation
23
/.scrutinizar.yml export-ignore
34
/.travis.yml export-ignore
45
/.gitignore export-ignore

Diff for: src/PHPHtmlParser/DTO/Selector/ParsedSelectorCollectionDTO.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@ final class ParsedSelectorCollectionDTO
1111
*/
1212
private $parsedSelectorDTO = [];
1313

14-
public function __construct(array $values)
14+
/**
15+
* @param ParsedSelectorDTO[] $parsedSelectorDTOs
16+
*/
17+
private function __construct(array $parsedSelectorDTOs)
1518
{
16-
foreach ($values as $value) {
17-
if ($value instanceof ParsedSelectorDTO) {
18-
$this->parsedSelectorDTO[] = $value;
19+
foreach ($parsedSelectorDTOs as $parsedSelectorDTO) {
20+
if ($parsedSelectorDTO instanceof ParsedSelectorDTO) {
21+
$this->parsedSelectorDTO[] = $parsedSelectorDTO;
1922
}
2023
}
2124
}
2225

26+
/**
27+
* @param ParsedSelectorDTO[] $parsedSelectorDTOs
28+
*/
29+
public static function makeCollection(array $parsedSelectorDTOs): ParsedSelectorCollectionDTO
30+
{
31+
return new ParsedSelectorCollectionDTO($parsedSelectorDTOs);
32+
}
33+
2334
/**
2435
* @return ParsedSelectorDTO[]
2536
*/

Diff for: src/PHPHtmlParser/DTO/Selector/ParsedSelectorDTO.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@ final class ParsedSelectorDTO
1111
*/
1212
private $rules = [];
1313

14-
public function __construct(array $values)
14+
/**
15+
* @param RuleDTO[] $ruleDTOs
16+
*/
17+
private function __construct(array $ruleDTOs)
1518
{
16-
foreach ($values as $value) {
17-
if ($value instanceof RuleDTO) {
18-
$this->rules[] = $value;
19+
foreach ($ruleDTOs as $ruleDTO) {
20+
if ($ruleDTO instanceof RuleDTO) {
21+
$this->rules[] = $ruleDTO;
1922
}
2023
}
2124
}
2225

26+
/**
27+
* @param RuleDTO[] $ruleDTOs
28+
*/
29+
public static function makeFromRules(array $ruleDTOs): ParsedSelectorDTO
30+
{
31+
return new ParsedSelectorDTO($ruleDTOs);
32+
}
33+
2334
/**
2435
* @return RuleDTO[]
2536
*/

Diff for: src/PHPHtmlParser/DTO/Selector/RuleDTO.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class RuleDTO
3636
*/
3737
private $alterNext;
3838

39-
public function __construct(array $values)
39+
private function __construct(array $values)
4040
{
4141
$this->tag = $values['tag'];
4242
$this->operator = $values['operator'];
@@ -47,16 +47,26 @@ public function __construct(array $values)
4747
}
4848

4949
/**
50-
* @return string
50+
* @param string|array|null $key
51+
* @param string|array|null $value
5152
*/
53+
public static function makeFromPrimitives(string $tag, string $operator, $key, $value, bool $noKey, bool $alterNext): RuleDTO
54+
{
55+
return new RuleDTO([
56+
'tag' => $tag,
57+
'operator' => $operator,
58+
'key' => $key,
59+
'value' => $value,
60+
'noKey' => $noKey,
61+
'alterNext' => $alterNext,
62+
]);
63+
}
64+
5265
public function getTag(): string
5366
{
5467
return $this->tag;
5568
}
5669

57-
/**
58-
* @return string
59-
*/
6070
public function getOperator(): string
6171
{
6272
return $this->operator;
@@ -78,17 +88,11 @@ public function getValue()
7888
return $this->value;
7989
}
8090

81-
/**
82-
* @return bool
83-
*/
8491
public function isNoKey(): bool
8592
{
8693
return $this->noKey;
8794
}
8895

89-
/**
90-
* @return bool
91-
*/
9296
public function isAlterNext(): bool
9397
{
9498
return $this->alterNext;

Diff for: src/PHPHtmlParser/DTO/Tag/AttributeDTO.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ final class AttributeDTO
1919
*/
2020
private $doubleQuote;
2121

22-
public function __construct(array $values)
22+
private function __construct(array $values)
2323
{
2424
$this->value = $values['value'];
2525
$this->doubleQuote = $values['doubleQuote'] ?? true;
2626
}
2727

28+
public static function makeFromPrimitives(?string $value, bool $doubleQuote = true): AttributeDTO
29+
{
30+
return new AttributeDTO([
31+
'value' => $value,
32+
'doubleQuote' => $doubleQuote,
33+
]);
34+
}
35+
2836
public function getValue(): ?string
2937
{
3038
return $this->value;

Diff for: src/PHPHtmlParser/DTO/TagDTO.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,24 @@ final class TagDTO
2828
*/
2929
private $tag;
3030

31-
public function __construct(array $values = [])
31+
private function __construct(array $values = [])
3232
{
3333
$this->status = $values['status'] ?? false;
3434
$this->closing = $values['closing'] ?? false;
3535
$this->node = $values['node'] ?? null;
3636
$this->tag = $values['tag'] ?? null;
3737
}
3838

39+
public static function makeFromPrimitives(bool $status = false, bool $closing = false, ?HtmlNode $node = null, ?string $tag = null): TagDTO
40+
{
41+
return new TagDTO([
42+
'status' => $status,
43+
'closing' => $closing,
44+
'node' => $node,
45+
'tag' => $tag,
46+
]);
47+
}
48+
3949
public function isStatus(): bool
4050
{
4151
return $this->status;

Diff for: src/PHPHtmlParser/Dom/Parser.php

+6-16
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,17 @@ public function detectCharset(Options $options, string $defaultCharset, Abstract
160160
*/
161161
private function parseTag(Options $options, Content $content, int $size): TagDTO
162162
{
163-
$return = [];
164163
if ($content->char() != '<') {
165164
// we are not at the beginning of a tag
166-
return new TagDTO();
165+
return TagDTO::makeFromPrimitives();
167166
}
168167

169168
// check if this is a closing tag
170169
try {
171170
$content->fastForward(1);
172171
} catch (ContentLengthException $exception) {
173172
// we are at the end of the file
174-
return new TagDTO();
173+
return TagDTO::makeFromPrimitives();
175174
}
176175
if ($content->char() == '/') {
177176
return $this->makeEndTag($content, $options);
@@ -188,7 +187,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
188187
$tag = \strtolower($content->copyByToken(StringToken::SLASH(), true));
189188
if (\trim($tag) == '') {
190189
// no tag found, invalid < found
191-
return new TagDTO();
190+
return TagDTO::makeFromPrimitives();
192191
}
193192
}
194193
$node = new HtmlNode($tag);
@@ -220,10 +219,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
220219
$content->fastForward(1);
221220
}
222221

223-
$return['status'] = true;
224-
$return['node'] = $node;
225-
226-
return new TagDTO($return);
222+
return TagDTO::makeFromPrimitives(true, false, $node);
227223
}
228224

229225
/**
@@ -249,7 +245,6 @@ private function detectHTML5Charset(Encode $encode, AbstractNode $root): bool
249245
*/
250246
private function makeEndTag(Content $content, Options $options): TagDTO
251247
{
252-
$return = [];
253248
$tag = $content->fastForward(1)
254249
->copyByToken(StringToken::SLASH(), true);
255250
// move to end of tag
@@ -259,15 +254,10 @@ private function makeEndTag(Content $content, Options $options): TagDTO
259254
// check if this closing tag counts
260255
$tag = \strtolower($tag);
261256
if (\in_array($tag, $options->getSelfClosing(), true)) {
262-
$return['status'] = true;
263-
264-
return new TagDTO($return);
257+
return TagDTO::makeFromPrimitives(true);
265258
}
266-
$return['status'] = true;
267-
$return['closing'] = true;
268-
$return['tag'] = \strtolower($tag);
269259

270-
return new TagDTO($return);
260+
return TagDTO::makeFromPrimitives(true, true, null, \strtolower($tag));
271261
}
272262

273263
/**

Diff for: src/PHPHtmlParser/Dom/Tag.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public function noise(string $noise): Tag
163163
*/
164164
public function setAttribute(string $key, ?string $attributeValue, bool $doubleQuote = true): Tag
165165
{
166-
$attributeDTO = new AttributeDTO([
167-
'value' => $attributeValue,
168-
'doubleQuote' => $doubleQuote,
169-
]);
166+
$attributeDTO = AttributeDTO::makeFromPrimitives(
167+
$attributeValue,
168+
$doubleQuote
169+
);
170170
if ($this->HtmlSpecialCharsDecode) {
171171
$attributeDTO->htmlspecialcharsDecode();
172172
}

Diff for: src/PHPHtmlParser/Selector/Parser.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,25 @@ public function parseSelectorString(string $selector): ParsedSelectorCollectionD
9292
$noKey = true;
9393
}
9494

95-
$rules[] = new RuleDTO([
96-
'tag' => $tag,
97-
'key' => $key,
98-
'value' => $value,
99-
'operator' => $operator,
100-
'noKey' => $noKey,
101-
'alterNext' => $alterNext,
102-
]);
95+
$rules[] = RuleDTO::makeFromPrimitives(
96+
$tag,
97+
$operator,
98+
$key,
99+
$value,
100+
$noKey,
101+
$alterNext
102+
);
103103
if (isset($match[7]) && \is_string($match[7]) && \trim($match[7]) == ',') {
104-
$selectors[] = new ParsedSelectorDTO($rules);
104+
$selectors[] = ParsedSelectorDTO::makeFromRules($rules);
105105
$rules = [];
106106
}
107107
}
108108

109109
// save last results
110110
if (\count($rules) > 0) {
111-
$selectors[] = new ParsedSelectorDTO($rules);
111+
$selectors[] = ParsedSelectorDTO::makeFromRules($rules);
112112
}
113113

114-
return new ParsedSelectorCollectionDTO($selectors);
114+
return ParsedSelectorCollectionDTO::makeCollection($selectors);
115115
}
116116
}

Diff for: tests/Selector/SeekerTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class SeekerTest extends TestCase
1010
{
1111
public function testSeekReturnEmptyArray()
1212
{
13-
$ruleDTO = new RuleDTO([
14-
'tag' => 'tag',
15-
'key' => 1,
16-
'value' => null,
17-
'operator' => null,
18-
'noKey' => false,
19-
'alterNext' => false,
20-
]);
13+
$ruleDTO = RuleDTO::makeFromPrimitives(
14+
'tag',
15+
'=',
16+
null,
17+
null,
18+
false,
19+
false
20+
);
2121
$seeker = new Seeker();
2222
$results = $seeker->seek([], $ruleDTO, []);
2323
$this->assertCount(0, $results);

0 commit comments

Comments
 (0)