Skip to content

Commit 5dc813c

Browse files
committed
Cleaned up the comments and php docs
1 parent 904aa90 commit 5dc813c

File tree

7 files changed

+70
-27
lines changed

7 files changed

+70
-27
lines changed

src/PHPHtmlParser/Content.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@ class Content {
3434
protected $slash = " />\r\n\t";
3535
protected $attr = ' >';
3636

37+
/**
38+
* Sets up the content block with its content.
39+
*
40+
* @param $content
41+
*/
3742
public function __construct($content)
3843
{
3944
$this->content = $content;
4045
$this->size = strlen($content);
4146
$this->pos = 0;
4247
}
4348

49+
/**
50+
* Returns the current position of the parser.
51+
*
52+
* @return int
53+
*/
4454
public function getPosition()
4555
{
4656
return $this->pos;
@@ -192,7 +202,7 @@ public function skip($string, $copy = false)
192202
{
193203
$len = strspn($this->content, $string, $this->pos);
194204

195-
// make it chainable if they don't want a copy
205+
// make it chain-able if they don't want a copy
196206
$return = $this;
197207
if ($copy)
198208
{

src/PHPHtmlParser/Dom.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PHPHtmlParser;
33

4+
use PHPHtmlPArser\Dom\AbstractNode;
45
use PHPHtmlParser\Dom\HtmlNode;
56
use PHPHtmlParser\Dom\TextNode;
67
use PHPHtmlParser\Exceptions\NotLoadedException;
@@ -363,7 +364,7 @@ protected function clean($str)
363364
$str = mb_eregi_replace("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>", '', $str);
364365
$str = mb_eregi_replace("<\s*style\s*>(.*?)<\s*/\s*style\s*>", '', $str);
365366

366-
// strip out preformatted tags
367+
// strip out pre-formatted tags
367368
$str = mb_eregi_replace("<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>", '', $str);
368369

369370
// strip out server side scripts
@@ -422,6 +423,7 @@ protected function parse()
422423
continue;
423424
}
424425

426+
/** @var AbstractNode $node */
425427
$node = $info['node'];
426428
$activeNode->addChild($node);
427429

@@ -575,7 +577,7 @@ protected function parseTag()
575577
if ( $this->options->strict)
576578
{
577579
$character = $this->content->getPosition();
578-
throw new StrictException("Tag '$tag' is not self clossing! (character #$character)");
580+
throw new StrictException("Tag '$tag' is not self closing! (character #$character)");
579581
}
580582

581583
// We force self closing on this tag.

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ abstract class AbstractNode {
5858
*/
5959
protected $encode;
6060

61+
/**
62+
* Creates a unique spl hash for this node.
63+
*/
6164
public function __construct()
6265
{
6366
$this->id = spl_object_hash($this);
@@ -179,7 +182,9 @@ public function propagateEncoding(Encode $encode)
179182
// check children
180183
foreach ($this->children as $id => $child)
181184
{
182-
$child['node']->propagateEncoding($encode);
185+
/** @var AbstractNode $node */
186+
$node = $child['node'];
187+
$node->propagateEncoding($encode);
183188
}
184189
}
185190

@@ -378,7 +383,7 @@ public function isChild ($id)
378383
}
379384

380385
/**
381-
* Checks if the given node id is a decendant of the
386+
* Checks if the given node id is a descendant of the
382387
* current node.
383388
*
384389
* @param int $id
@@ -393,12 +398,12 @@ public function isDescendant($id)
393398

394399
foreach ($this->children as $childId => $child)
395400
{
396-
if ($child['node']->hasChildren())
401+
/** @var AbstractNode $node */
402+
$node = $child['node'];
403+
if ($node->hasChildren() &&
404+
$node->isDescendant($id))
397405
{
398-
if ($child['node']->isDescendant($id))
399-
{
400-
return true;
401-
}
406+
return true;
402407
}
403408
}
404409

@@ -591,7 +596,7 @@ public function ancestorByTag($tag)
591596
*
592597
* @param string $selector
593598
* @param int $nth
594-
* @return array
599+
* @return array|AbstractNode
595600
*/
596601
public function find($selector, $nth = null)
597602
{
@@ -619,10 +624,10 @@ public function find($selector, $nth = null)
619624
*
620625
* Far future enhancement
621626
* Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width
622-
* Note that in this case, the class or id will have the img subselector for it to apply to the image.
627+
* Note that in this case, the class or id will have the img sub-selector for it to apply to the image.
623628
*
624629
* ridiculously far future development
625-
* If the class or id is specified in a SEPARATE css file thats not on the page, go get it and do what we were just doing for the ones on the page.
630+
* If the class or id is specified in a SEPARATE css file that's not on the page, go get it and do what we were just doing for the ones on the page.
626631
*
627632
* @author John Schlick
628633
* @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out.
@@ -651,9 +656,9 @@ public function get_display_size()
651656
// Now look for an inline style.
652657
if ( ! is_null($this->tag->getAttribute('style')))
653658
{
654-
// Thanks to user gnarf from stackoverflow for this regular expression.
659+
// Thanks to user 'gnarf' from stackoverflow for this regular expression.
655660
$attributes = [];
656-
preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->tag->getAttribute['style'], $matches, PREG_SET_ORDER);
661+
preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->tag->getAttribute('style'), $matches, PREG_SET_ORDER);
657662
foreach ($matches as $match)
658663
{
659664
$attributes[$match[1]] = $match[2];

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ public function text($lookInChildren = false)
170170
$text = '';
171171
foreach ($this->children as $child)
172172
{
173-
if ($child['node'] instanceof TextNode)
173+
/** @var AbstractNode $node */
174+
$node = $child['node'];
175+
if ($node instanceof TextNode)
174176
{
175177
$text .= $child['node']->text;
176178
}
177179
elseif($lookInChildren and
178-
$child['node'] instanceof HtmlNode)
180+
$node instanceof HtmlNode)
179181
{
180-
$text .= $child['node']->text($lookInChildren);
182+
$text .= $node->text($lookInChildren);
181183
}
182184
}
183185

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Tag {
2525
*
2626
* @var bool
2727
*/
28-
protected $selfclosing = false;
28+
protected $selfClosing = false;
2929

3030
/**
3131
* Tag noise
@@ -39,16 +39,33 @@ class Tag {
3939
*/
4040
protected $encode = null;
4141

42+
/**
43+
* Sets up the tag with a name.
44+
*
45+
* @param $name
46+
*/
4247
public function __construct($name)
4348
{
4449
$this->name = $name;
4550
}
4651

52+
/**
53+
* Magic method to get any of the attributes.
54+
*
55+
* @param string $key
56+
* @return mixed
57+
*/
4758
public function __get($key)
4859
{
4960
return$this->getAttribute($key);
5061
}
5162

63+
/**
64+
* Magic method to set any attribute.
65+
*
66+
* @param string $key
67+
* @param mixed $value
68+
*/
5269
public function __set($key, $value)
5370
{
5471
$this->setAttribute($key, $value);
@@ -71,7 +88,7 @@ public function name()
7188
*/
7289
public function selfClosing()
7390
{
74-
$this->selfclosing = true;
91+
$this->selfClosing = true;
7592
return $this;
7693
}
7794

@@ -82,9 +99,14 @@ public function selfClosing()
8299
*/
83100
public function isSelfClosing()
84101
{
85-
return $this->selfclosing;
102+
return $this->selfClosing;
86103
}
87104

105+
/**
106+
* Sets the encoding type to be used.
107+
*
108+
* @param Encode $encode
109+
*/
88110
public function setEncoding(Encode $encode)
89111
{
90112
$this->encode = $encode;
@@ -205,7 +227,7 @@ public function makeOpeningTag()
205227
}
206228
}
207229

208-
if ($this->selfclosing)
230+
if ($this->selfClosing)
209231
{
210232
return $return.' />';
211233
}
@@ -222,7 +244,7 @@ public function makeOpeningTag()
222244
*/
223245
public function makeClosingTag()
224246
{
225-
if ($this->selfclosing)
247+
if ($this->selfClosing)
226248
{
227249
return '';
228250
}

src/PHPHtmlParser/Selector.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Selector {
99

1010
/**
11-
* Pattern of CSS selectors, modified from mootools
11+
* Pattern of CSS selectors, modified from 'mootools'
1212
*
1313
* @var string
1414
*/
@@ -117,7 +117,7 @@ protected function parseSelectorString($selector)
117117
$value = $match[3];
118118
}
119119

120-
// and final attribute selecter
120+
// and final attribute selector
121121
if ( ! empty($match[4]))
122122
{
123123
$key = strtolower($match[4]);
@@ -177,6 +177,7 @@ protected function seek(array $nodes, array $rule, array $options)
177177
is_numeric($rule['key']))
178178
{
179179
$count = 0;
180+
/** @var AbstractNode $node */
180181
foreach ($nodes as $node)
181182
{
182183
if ($rule['tag'] == '*' OR $rule['tag'] == $node->getTag()->name())
@@ -195,6 +196,7 @@ protected function seek(array $nodes, array $rule, array $options)
195196
$options = $this->flattenOptions($options);
196197

197198
$return = [];
199+
/** @var AbstractNode $node */
198200
foreach ($nodes as $node)
199201
{
200202
// check if we are a leaf
@@ -226,7 +228,7 @@ protected function seek(array $nodes, array $rule, array $options)
226228
if ( ! empty($rule['tag']) AND $rule['tag'] != $child->getTag()->name() AND
227229
$rule['tag'] != '*')
228230
{
229-
// child faild tag check
231+
// child failed tag check
230232
$pass = false;
231233
}
232234

tests/Options/StrictTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testConfigStrictMissingSelfClosing()
3030
}
3131
catch (StrictException $e)
3232
{
33-
$this->assertEquals("Tag 'br' is not self clossing! (character #31)", $e->getMessage());
33+
$this->assertEquals("Tag 'br' is not self closing! (character #31)", $e->getMessage());
3434
}
3535
}
3636

0 commit comments

Comments
 (0)