Skip to content

Commit 81d3f58

Browse files
committed
change a lot of is_null references to "=== null", also rewritten char() in Content-class for more performance
1 parent 668c770 commit 81d3f58

File tree

5 files changed

+79
-88
lines changed

5 files changed

+79
-88
lines changed

src/PHPHtmlParser/Content.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
declare(strict_types=1);
44

@@ -76,16 +76,7 @@ public function getPosition(): int
7676
*/
7777
public function char(?int $char = null): string
7878
{
79-
$pos = $this->pos;
80-
if ( ! is_null($char)) {
81-
$pos = $char;
82-
}
83-
84-
if ( ! isset($this->content[$pos])) {
85-
return '';
86-
}
87-
88-
return $this->content[$pos];
79+
return $this->content[$char ?? $this->pos] ?? '';
8980
}
9081

9182
/**
@@ -137,7 +128,7 @@ public function copyUntil(string $string, bool $char = false, bool $escape = fal
137128
if ($escape) {
138129
$position = $this->pos;
139130
$found = false;
140-
while ( ! $found) {
131+
while (!$found) {
141132
$position = strpos($this->content, $string, $position);
142133
if ($position === false) {
143134
// reached the end
@@ -163,7 +154,7 @@ public function copyUntil(string $string, bool $char = false, bool $escape = fal
163154
// could not find character, just return the remaining of the content
164155
$return = substr($this->content, $this->pos, $this->size - $this->pos);
165156
if ($return === false) {
166-
throw new LogicalException('Substr returned false with position '.$this->pos.'.');
157+
throw new LogicalException('Substr returned false with position ' . $this->pos . '.');
167158
}
168159
$this->pos = $this->size;
169160

@@ -177,7 +168,7 @@ public function copyUntil(string $string, bool $char = false, bool $escape = fal
177168

178169
$return = substr($this->content, $this->pos, $position - $this->pos);
179170
if ($return === false) {
180-
throw new LogicalException('Substr returned false with position '.$this->pos.'.');
171+
throw new LogicalException('Substr returned false with position ' . $this->pos . '.');
181172
}
182173
// set the new position
183174
$this->pos = $position;
@@ -201,7 +192,7 @@ public function copyUntilUnless(string $string, string $unless)
201192

202193
$position = strcspn($foundString, $unless);
203194
if ($position == strlen($foundString)) {
204-
return $string.$foundString;
195+
return $string . $foundString;
205196
}
206197
// rewind changes and return nothing
207198
$this->pos = $lastPos;
@@ -241,7 +232,7 @@ public function skip(string $string, bool $copy = false)
241232
if ($copy) {
242233
$return = substr($this->content, $this->pos, $len);
243234
if ($return === false) {
244-
throw new LogicalException('Substr returned false with position '.$this->pos.'.');
235+
throw new LogicalException('Substr returned false with position ' . $this->pos . '.');
245236
}
246237
}
247238

src/PHPHtmlParser/Dom.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace PHPHtmlParser;
36

47
use PHPHtmlParser\Dom\AbstractNode;
@@ -174,7 +177,7 @@ public function loadFromFile(string $file, array $options = []): Dom
174177
{
175178
$content = file_get_contents($file);
176179
if ($content === false) {
177-
throw new LogicalException('file_get_contents failed and returned false when trying to read "'.$file.'".');
180+
throw new LogicalException('file_get_contents failed and returned false when trying to read "' . $file . '".');
178181
}
179182
return $this->loadStr($content, $options);
180183
}
@@ -193,7 +196,7 @@ public function loadFromFile(string $file, array $options = []): Dom
193196
*/
194197
public function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
195198
{
196-
if (is_null($curl)) {
199+
if ($curl === null) {
197200
// use the default curl interface
198201
$curl = new Curl;
199202
}
@@ -216,7 +219,7 @@ public function loadStr(string $str, array $option = []): Dom
216219
{
217220
$this->options = new Options;
218221
$this->options->setOptions($this->globalOptions)
219-
->setOptions($option);
222+
->setOptions($option);
220223

221224
$this->rawSize = strlen($str);
222225
$this->raw = $str;
@@ -293,7 +296,7 @@ public function findById(int $id)
293296
*/
294297
public function addSelfClosingTag($tag): Dom
295298
{
296-
if ( ! is_array($tag)) {
299+
if (!is_array($tag)) {
297300
$tag = [$tag];
298301
}
299302
foreach ($tag as $value) {
@@ -313,7 +316,7 @@ public function addSelfClosingTag($tag): Dom
313316
*/
314317
public function removeSelfClosingTag($tag): Dom
315318
{
316-
if ( ! is_array($tag)) {
319+
if (!is_array($tag)) {
317320
$tag = [$tag];
318321
}
319322
$this->selfClosing = array_diff($this->selfClosing, $tag);
@@ -344,7 +347,7 @@ public function clearSelfClosingTags(): Dom
344347
*/
345348
public function addNoSlashTag($tag): Dom
346349
{
347-
if ( ! is_array($tag)) {
350+
if (!is_array($tag)) {
348351
$tag = [$tag];
349352
}
350353
foreach ($tag as $value) {
@@ -363,7 +366,7 @@ public function addNoSlashTag($tag): Dom
363366
*/
364367
public function removeNoSlashTag($tag): Dom
365368
{
366-
if ( ! is_array($tag)) {
369+
if (!is_array($tag)) {
367370
$tag = [$tag];
368371
}
369372
$this->noSlash = array_diff($this->noSlash, $tag);
@@ -461,7 +464,7 @@ public function getElementById($id)
461464
{
462465
$this->isLoaded();
463466

464-
return $this->find('#'.$id, 0);
467+
return $this->find('#' . $id, 0);
465468
}
466469

467470
/**
@@ -491,7 +494,7 @@ public function getElementsByClass(string $class)
491494
{
492495
$this->isLoaded();
493496

494-
return $this->find('.'.$class);
497+
return $this->find('.' . $class);
495498
}
496499

497500
/**
@@ -622,11 +625,11 @@ protected function parse(): void
622625
$this->root = new HtmlNode('root');
623626
$this->root->setHtmlSpecialCharsDecode($this->options->htmlSpecialCharsDecode);
624627
$activeNode = $this->root;
625-
while ( ! is_null($activeNode)) {
628+
while ($activeNode !== null) {
626629
$str = $this->content->copyUntil('<');
627630
if ($str == '') {
628631
$info = $this->parseTag();
629-
if ( ! $info['status']) {
632+
if (!$info['status']) {
630633
// we are done here
631634
$activeNode = null;
632635
continue;
@@ -638,7 +641,7 @@ protected function parse(): void
638641
$originalNode = $activeNode;
639642
while ($activeNode->getTag()->name() != $info['tag']) {
640643
$activeNode = $activeNode->getParent();
641-
if (is_null($activeNode)) {
644+
if ($activeNode === null) {
642645
// we could not find opening tag
643646
$activeNode = $originalNode;
644647
$foundOpeningTag = false;
@@ -651,7 +654,7 @@ protected function parse(): void
651654
continue;
652655
}
653656

654-
if ( ! isset($info['node'])) {
657+
if (!isset($info['node'])) {
655658
continue;
656659
}
657660

@@ -660,10 +663,11 @@ protected function parse(): void
660663
$activeNode->addChild($node);
661664

662665
// check if node is self closing
663-
if ( ! $node->getTag()->isSelfClosing()) {
666+
if (!$node->getTag()->isSelfClosing()) {
664667
$activeNode = $node;
665668
}
666-
} else if ($this->options->whitespaceTextNode ||
669+
} elseif (
670+
$this->options->whitespaceTextNode ||
667671
trim($str) != ''
668672
) {
669673
// we found text we care about
@@ -696,7 +700,7 @@ protected function parseTag(): array
696700
if ($this->content->fastForward(1)->char() == '/') {
697701
// end tag
698702
$tag = $this->content->fastForward(1)
699-
->copyByToken('slash', true);
703+
->copyByToken('slash', true);
700704
// move to end of tag
701705
$this->content->copyUntil('>');
702706
$this->content->fastForward(1);
@@ -717,17 +721,18 @@ protected function parseTag(): array
717721
}
718722

719723
$tag = strtolower($this->content->copyByToken('slash', true));
720-
if (trim($tag) == '')
721-
{
724+
if (trim($tag) == '') {
722725
// no tag found, invalid < found
723726
return $return;
724727
}
725728
$node = new HtmlNode($tag);
726729
$node->setHtmlSpecialCharsDecode($this->options->htmlSpecialCharsDecode);
727730

728731
// attributes
729-
while ($this->content->char() != '>' &&
730-
$this->content->char() != '/') {
732+
while (
733+
$this->content->char() != '>' &&
734+
$this->content->char() != '/'
735+
) {
731736
$space = $this->content->skipByToken('blank', true);
732737
if (empty($space)) {
733738
$this->content->fastForward(1);
@@ -740,15 +745,15 @@ protected function parseTag(): array
740745
}
741746

742747
if (empty($name)) {
743-
$this->content->skipByToken('blank');
744-
continue;
748+
$this->content->skipByToken('blank');
749+
continue;
745750
}
746751

747752
$this->content->skipByToken('blank');
748753
if ($this->content->char() == '=') {
749754
$attr = [];
750755
$this->content->fastForward(1)
751-
->skipByToken('blank');
756+
->skipByToken('blank');
752757
switch ($this->content->char()) {
753758
case '"':
754759
$attr['doubleQuote'] = true;
@@ -757,7 +762,7 @@ protected function parseTag(): array
757762
do {
758763
$moreString = $this->content->copyUntilUnless('"', '=>');
759764
$string .= $moreString;
760-
} while ( ! empty($moreString));
765+
} while (!empty($moreString));
761766
$attr['value'] = $string;
762767
$this->content->fastForward(1);
763768
$node->getTag()->$name = $attr;
@@ -769,7 +774,7 @@ protected function parseTag(): array
769774
do {
770775
$moreString = $this->content->copyUntilUnless("'", '=>');
771776
$string .= $moreString;
772-
} while ( ! empty($moreString));
777+
} while (!empty($moreString));
773778
$attr['value'] = $string;
774779
$this->content->fastForward(1);
775780
$node->getTag()->$name = $attr;
@@ -815,11 +820,9 @@ protected function parseTag(): array
815820
$node->getTag()->selfClosing();
816821

817822
// Should this tag use a trailing slash?
818-
if(in_array($tag, $this->noSlash, true))
819-
{
823+
if (in_array($tag, $this->noSlash, true)) {
820824
$node->getTag()->noTrailingSlash();
821825
}
822-
823826
}
824827

825828
$this->content->fastForward(1);
@@ -844,7 +847,7 @@ protected function detectCharset(): bool
844847
$encode->to($this->defaultCharset);
845848

846849
$enforceEncoding = $this->options->enforceEncoding;
847-
if ( ! is_null($enforceEncoding)) {
850+
if (!is_null($enforceEncoding)) {
848851
// they want to enforce the given encoding
849852
$encode->from($enforceEncoding);
850853
$encode->to($enforceEncoding);

0 commit comments

Comments
 (0)