Skip to content

Commit

Permalink
Use type hints instead of manual type checking. Unify exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Imangazaliev committed May 8, 2022
1 parent f0de722 commit ff5c7f1
Show file tree
Hide file tree
Showing 19 changed files with 475 additions and 939 deletions.
38 changes: 14 additions & 24 deletions src/DiDom/ClassAttribute.php
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php <?php


declare(strict_types=1);

namespace DiDom; namespace DiDom;


use InvalidArgumentException; use InvalidArgumentException;
Expand Down Expand Up @@ -31,7 +33,7 @@ class ClassAttribute
public function __construct(Element $element) public function __construct(Element $element)
{ {
if ( ! $element->isElementNode()) { if ( ! $element->isElementNode()) {
throw new InvalidArgumentException(sprintf('The element must contain DOMElement node')); throw new InvalidArgumentException(sprintf('The element must contain DOMElement node.'));
} }


$this->element = $element; $this->element = $element;
Expand Down Expand Up @@ -96,12 +98,8 @@ protected function updateClassAttribute()
* *
* @throws InvalidArgumentException if class name is not a string * @throws InvalidArgumentException if class name is not a string
*/ */
public function add($className) public function add(string $className): self
{ {
if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}

$this->parseClassAttribute(); $this->parseClassAttribute();


if (in_array($className, $this->classes, true)) { if (in_array($className, $this->classes, true)) {
Expand All @@ -122,13 +120,13 @@ public function add($className)
* *
* @throws InvalidArgumentException if class name is not a string * @throws InvalidArgumentException if class name is not a string
*/ */
public function addMultiple(array $classNames) public function addMultiple(array $classNames): self
{ {
$this->parseClassAttribute(); $this->parseClassAttribute();


foreach ($classNames as $className) { foreach ($classNames as $className) {
if ( ! is_string($className)) { if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className)))); throw new InvalidArgumentException(sprintf('Class name must be a string, %s given.', (is_object($className) ? get_class($className) : gettype($className))));
} }


if (in_array($className, $this->classes, true)) { if (in_array($className, $this->classes, true)) {
Expand All @@ -146,7 +144,7 @@ public function addMultiple(array $classNames)
/** /**
* @return string[] * @return string[]
*/ */
public function getAll() public function getAll(): array
{ {
$this->parseClassAttribute(); $this->parseClassAttribute();


Expand All @@ -158,12 +156,8 @@ public function getAll()
* *
* @return bool * @return bool
*/ */
public function contains($className) public function contains(string $className): bool
{ {
if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}

$this->parseClassAttribute(); $this->parseClassAttribute();


return in_array($className, $this->classes, true); return in_array($className, $this->classes, true);
Expand All @@ -176,12 +170,8 @@ public function contains($className)
* *
* @throws InvalidArgumentException if class name is not a string * @throws InvalidArgumentException if class name is not a string
*/ */
public function remove($className) public function remove(string $className): self
{ {
if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}

$this->parseClassAttribute(); $this->parseClassAttribute();


$classIndex = array_search($className, $this->classes); $classIndex = array_search($className, $this->classes);
Expand All @@ -204,13 +194,13 @@ public function remove($className)
* *
* @throws InvalidArgumentException if class name is not a string * @throws InvalidArgumentException if class name is not a string
*/ */
public function removeMultiple(array $classNames) public function removeMultiple(array $classNames): self
{ {
$this->parseClassAttribute(); $this->parseClassAttribute();


foreach ($classNames as $className) { foreach ($classNames as $className) {
if ( ! is_string($className)) { if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className)))); throw new InvalidArgumentException(sprintf('Class name must be a string, %s given.', (is_object($className) ? get_class($className) : gettype($className))));
} }


$classIndex = array_search($className, $this->classes); $classIndex = array_search($className, $this->classes);
Expand All @@ -232,15 +222,15 @@ public function removeMultiple(array $classNames)
* *
* @return ClassAttribute * @return ClassAttribute
*/ */
public function removeAll(array $exclusions = []) public function removeAll(array $exclusions = []): self
{ {
$this->parseClassAttribute(); $this->parseClassAttribute();


$preservedClasses = []; $preservedClasses = [];


foreach ($exclusions as $className) { foreach ($exclusions as $className) {
if ( ! is_string($className)) { if ( ! is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className)))); throw new InvalidArgumentException(sprintf('Class name must be a string, %s given.', (is_object($className) ? get_class($className) : gettype($className))));
} }


if ( ! in_array($className, $this->classes, true)) { if ( ! in_array($className, $this->classes, true)) {
Expand All @@ -260,7 +250,7 @@ public function removeAll(array $exclusions = [])
/** /**
* @return Element * @return Element
*/ */
public function getElement() public function getElement(): Element
{ {
return $this->element; return $this->element;
} }
Expand Down
Loading

0 comments on commit ff5c7f1

Please sign in to comment.