Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality Improvments #30

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions php_cs.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
$config = new PhpCsFixer\Config();
return $config->setRules([
'align_multiline_comment' => [
'comment_type' => 'phpdocs_only'
],
Expand Down Expand Up @@ -54,7 +54,7 @@ return PhpCsFixer\Config::create()
'sort_algorithm' => 'alpha'
],
'ordered_class_elements' => [
'sortAlgorithm' => 'alpha',
'sort_algorithm' => 'alpha',
'order' => [
'constant_public',
'constant_protected',
Expand Down
2 changes: 1 addition & 1 deletion webfiori/ui/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function setTarget($name) {
*
* @since 1.0
*/
public function setText(string $text,$escHtmlEntities = true) : HTMLNode {
public function setText(string $text, bool $escHtmlEntities = true) : HTMLNode {
$node = $this->getChild(0);

if ($node->getNodeName() == '#TEXT') {
Expand Down
12 changes: 6 additions & 6 deletions webfiori/ui/CodeSnippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CodeSnippet extends HTMLNode {
*
* @since 1.0
*/
public function __construct($title = 'Code Snippit', $code = null) {
public function __construct($title = 'Code Snippit',string $code = null) {
parent::__construct();
$this->originalCode = '';
$this->codeStrNode = HTMLNode::createTextNode('');
Expand Down Expand Up @@ -162,7 +162,7 @@ public function __construct($title = 'Code Snippit', $code = null) {
$this->addChild($this->codeDisplay);
$this->codeDisplay->addChild($this->pre);
$this->pre->addChild($this->code);
$this->_addLine();
$this->addLineHelper();
$this->setCode($code);
$this->setTitle($title);
}
Expand All @@ -176,7 +176,7 @@ public function __construct($title = 'Code Snippit', $code = null) {
*/
public function addCodeLine($codeAsTxt) {
$this->originalCode .= $codeAsTxt;
$this->_addLine();
$this->addLineHelper();
$oldCode = $this->codeStrNode->getTextUnescaped();
$oldCode .= trim($codeAsTxt,"\n\r")."\n";
$this->codeStrNode->setText($oldCode);
Expand Down Expand Up @@ -239,12 +239,12 @@ public function setCode($code) {
if ($len !== 0) {
for ($x = 0 ; $x < $len ; $x++) {
if ($xCode[$x] == "\n") {
$this->_addLine();
$this->addLineHelper();
}
}
}
$this->codeStrNode->setText($xCode."\n");
$this->_addLine();
$this->addLineHelper();
}
/**
* Sets the title of the snippit.
Expand All @@ -263,7 +263,7 @@ public function setTitle(string $title) : HTMLNode {

return $this;
}
private function _addLine() {
private function addLineHelper() {
$span = new HTMLNode('span');
$span->setClassName('line-number');
$span->setAttribute('style', ''
Expand Down
77 changes: 48 additions & 29 deletions webfiori/ui/HTMLDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __toString() {
*
* @since 1.0
*/
public function addChild($node, array $attributes = [], $chainOnParent = false) {
public function addChild($node, array $attributes = [], bool $chainOnParent = false) {
$name = $node instanceof HTMLNode ? $node->getNodeName() : trim($node);

if ($name != 'body' && $name != 'head' && $name != 'html') {
Expand Down Expand Up @@ -176,7 +176,7 @@ public function asCode(array $formattingOptions = HTMLNode::DEFAULT_CODE_FORMAT)
*
* @since 1.2
*/
public function getBody() {
public function getBody() : HTMLNode {
return $this->body;
}
/**
Expand Down Expand Up @@ -205,11 +205,11 @@ public function getChildByID($id) {
*
* @since 1.4.2
*/
public function getChildrenByAttributeValue($attrName, $attrVal) {
public function getChildrenByAttributeValue(string $attrName, string $attrVal) : LinkedList {
$list = new LinkedList();
$trimmedAttrName = trim($attrName);
$trimmedVal = trim($attrVal);
$this->_getChildrenByAttributeValue($trimmedAttrName, $trimmedVal, $list, $this->getDocumentRoot());
$this->getChildrenByAttributeValueHelper($trimmedAttrName, $trimmedVal, $list, $this->getDocumentRoot());

return $list;
}
Expand All @@ -224,12 +224,12 @@ public function getChildrenByAttributeValue($attrName, $attrVal) {
*
* @since 1.2
*/
public function getChildrenByTag($val) {
public function getChildrenByTag(string $val) : LinkedList {
$list = new LinkedList();
$trimmedVal = strtolower(trim($val));

if (strlen($trimmedVal) != 0) {
$this->_getChildrenByTag($trimmedVal, $list, $this->getDocumentRoot());
$this->getChildrenByTagHelper($trimmedVal, $list, $this->getDocumentRoot());
}

return $list;
Expand All @@ -243,7 +243,7 @@ public function getChildrenByTag($val) {
*
* @since 1.4.1
*/
public function getDocumentRoot() {
public function getDocumentRoot() : HTMLNode {
return $this->htmlNode;
}
/**
Expand All @@ -253,7 +253,7 @@ public function getDocumentRoot() {
*
* @since 1.2
*/
public function getHeadNode() {
public function getHeadNode() : HeadNode {
return $this->headNode;
}
/**
Expand All @@ -264,7 +264,7 @@ public function getHeadNode() {
*
* @since 1.0
*/
public function getLanguage() {
public function getLanguage() : string {
if ($this->getDocumentRoot()->hasAttribute('lang')) {
return $this->getDocumentRoot()->getAttributeValue('lang');
}
Expand All @@ -284,14 +284,33 @@ public function getLanguage() {
*/
public function removeChild($node) {
if ($node instanceof HTMLNode && $node !== $this->body && $node !== $this->headNode) {
return $this->_removeChild($this->getDocumentRoot(), $node);
return $this->removeChildHelper($this->getDocumentRoot(), $node);
} else if (gettype($node) == 'string') {
$toRemove = $this->getDocumentRoot()->getChildByID($node);
if ($toRemove !== $this->body && $toRemove !== $this->headNode) {
return $this->_removeChild($this->getDocumentRoot(), $toRemove);
}
return $this->removeChildByID($node);
}

return null;
}
/**
* Removes a child element given its ID.
*
* The element will be removed only if it is exist and not one of the
* following elements: 'body' and, 'head'.
*
* @param string $id The value of the attribute 'id' of the element.
*
* @return HTMLNode|null If the element is removed, an object is
* returned that holds the information of the element. Other than that,
* null is returned.
*/
public function removeChildByID(string $id) {
$toRemove = $this->getDocumentRoot()->getChildByID($id);

if ($toRemove !== $this->body && $toRemove !== $this->headNode) {
return $this->removeChildHelper($this->getDocumentRoot(), $toRemove);
}


return null;
}
/**
Expand All @@ -306,12 +325,12 @@ public function removeChild($node) {
* @param bool $wellFormatted If set to true, The generated file will be
* well formatted (readable by humans).
*
* @return boolean The method will return true if the file is successfully created.
* @return bool The method will return true if the file is successfully created.
* False if not. Default is true.
*
* @since 1.0
*/
public function saveToHTMLFile($path,$fileName,$wellFormatted = true) {
public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatted = true) {
$trimmedPath = trim($path);
$trimmedName = trim($fileName);

Expand All @@ -333,12 +352,12 @@ public function saveToHTMLFile($path,$fileName,$wellFormatted = true) {
*
* @param HeadNode $node The node to set.
*
* @return boolean If head node is set, the method will return true.
* @return bool If head node is set, the method will return true.
* if it is not set, the method will return false.
*
* @since 1.0
*/
public function setHeadNode($node) {
public function setHeadNode(HeadNode $node) : bool {
if ($node instanceof HeadNode && $this->getDocumentRoot()->replaceChild($this->headNode, $node)) {
$this->headNode = $node;

Expand All @@ -354,13 +373,13 @@ public function setHeadNode($node) {
* empty or its length does not equal to 2, language won't be set. If null
* is given, then the attribute will be removed if it was set.
*
* @return boolean If the attribute 'lang' of the document is set, or
* @return bool If the attribute 'lang' of the document is set, or
* removed, the method will return true. Note that the method will always
* return true if null is given. Other than that, it will return false.
*
* @since 1.0
*/
public function setLanguage($lang) {
public function setLanguage(string $lang = null) {
if ($lang === null) {
$this->getDocumentRoot()->removeAttribute('lang');

Expand Down Expand Up @@ -388,7 +407,7 @@ public function setLanguage($lang) {
*
* @since 1.0
*/
public function toHTML($formatted = true) {
public function toHTML(bool $formatted = true) : string {
if (!$formatted) {
$this->nl = '';
} else {
Expand All @@ -401,19 +420,19 @@ public function toHTML($formatted = true) {
}
/**
*
* @param type $attr
* @param type $val
* @param string $attr
* @param string $val
* @param LinkedList $list
* @param HTMLNode $el
*/
private function _getChildrenByAttributeValue($attr, $val, LinkedList $list, HTMLNode $el) {
private function getChildrenByAttributeValueHelper(string $attr, string $val, LinkedList $list, HTMLNode $el) {
if ($el->getAttribute($attr) == $val) {
$list->add($el);
}

if ($el->children() !== null) {
foreach ($el->children() as $child) {
$this->_getChildrenByAttributeValue($attr, $val, $list, $child);
$this->getChildrenByAttributeValueHelper($attr, $val, $list, $child);
}
}
}
Expand All @@ -423,7 +442,7 @@ private function _getChildrenByAttributeValue($attr, $val, LinkedList $list, HTM
* @param LinkedList $list
* @param HTMLNode $child
*/
private function _getChildrenByTag($val,$list,$child) {
private function getChildrenByTagHelper($val,$list,$child) {
if ($child->getNodeName() == $val) {
$list->add($child);
}
Expand All @@ -434,7 +453,7 @@ private function _getChildrenByTag($val,$list,$child) {

for ($x = 0 ; $x < $chCount ; $x++) {
$ch = $children->get($x);
$this->_getChildrenByTag($val, $list, $ch);
$this->getChildrenByTagHelper($val, $list, $ch);
}
}
}
Expand All @@ -443,9 +462,9 @@ private function _getChildrenByTag($val,$list,$child) {
* @param HTMLNode $ch
* @param HTMLNode $nodeToRemove Description
*/
private function _removeChild($ch,$nodeToRemove) {
private function removeChildHelper($ch,$nodeToRemove) {
for ($x = 0 ; $x < $ch->childrenCount() ; $x++) {
$removed = $this->_removeChild($ch->children()->get($x),$nodeToRemove);
$removed = $this->removeChildHelper($ch->children()->get($x),$nodeToRemove);

if ($removed instanceof HTMLNode) {
return $removed;
Expand Down
12 changes: 5 additions & 7 deletions webfiori/ui/HTMLList.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(string $listType = 'ul', array $arrOfItems = [], boo
* class 'HTMLNode' or a string that represents the
* content of the list item that will be added.
*
* @param array|boolean $attrs An optional array of attributes which will be set in
* @param array|bool $attrs An optional array of attributes which will be set in
* the newly added list item.
*
* @param bool $chainOnParent If this parameter is set to true, the method
Expand Down Expand Up @@ -154,12 +154,10 @@ public function addListItems(array $arrOfItems, bool $escHtmlEntities = true) :
*
* @since 1.0
*/
public function addSubList($ul) : HTMLList {
if ($ul instanceof HTMLList) {
$li = new ListItem();
$li->addList($ul);
$this->addChild($li);
}
public function addSubList(HTMLList $ul) : HTMLList {
$li = new ListItem();
$li->addList($ul);
$this->addChild($li);

return $this;
}
Expand Down
Loading