Skip to content

Upgrading a minimum PHP version to 8.1 #12

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
}
],
"require":{
"php":">=5.6",
"php":">=8.1",
"ext-dom": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"symfony/css-selector": "*"
"symfony/css-selector": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^10.0.0",
"php-coveralls/php-coveralls": "^2.2"
},
"autoload": {
Expand Down
61 changes: 26 additions & 35 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class Document
/**
* @var DOMDocument
*/
protected $document;
protected DOMDocument $document;

/**
* @var array
*/
protected $functionAliases = [
protected array $functionAliases = [
'outertext' => 'html',
'innertext' => 'innerHtml',
'load' => 'loadHtml',
Expand All @@ -52,7 +52,7 @@ class Document
*
* @param string|Element $element HTML code or Element
*/
public function __construct($element = null)
public function __construct(mixed $element = null)
{
$this->document = new DOMDocument('1.0', 'UTF-8');

Expand All @@ -78,7 +78,7 @@ public function __construct($element = null)
* @return Document
* @throws InvalidArgumentException if argument is not string
*/
public function loadHtml($html)
public function loadHtml(mixed $html): static
{
if (!is_string($html)) {
throw new InvalidArgumentException(__METHOD__ . ' expects parameter 1 to be string.');
Expand Down Expand Up @@ -108,7 +108,7 @@ public function loadHtml($html)
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function loadHtmlFile($filePath)
public function loadHtmlFile(mixed $filePath): static
{
if (!is_string($filePath)) {
throw new InvalidArgumentException(__METHOD__ . ' expects parameter 1 to be string.');
Expand All @@ -118,15 +118,9 @@ public function loadHtmlFile($filePath)
throw new RuntimeException("File $filePath not found");
}

try {
$html = file_get_contents($filePath);
} catch (\Exception $e) {
throw new RuntimeException("Could not load file $filePath");
}

if ($html === false) {
throw new RuntimeException("Could not load file $filePath");
}
if (($html = @file_get_contents($filePath)) === false) {
throw new RuntimeException("Could not load file $filePath");
}

$this->loadHtml($html);

Expand All @@ -136,7 +130,7 @@ public function loadHtmlFile($filePath)
/**
* @return DOMDocument
*/
public function getDocument()
public function getDocument(): DOMDocument
{
return $this->document;
}
Expand All @@ -145,11 +139,11 @@ public function getDocument()
* Find list of nodes with a CSS selector
*
* @param string $selector
* @param int $idx
* @param int|null $idx
*
* @return NodeList|Element|null
*/
public function find($selector, $idx = null)
public function find(mixed $selector, int $idx = null): NodeList|Element|null
{
$xPathQuery = SelectorConverter::toXPath($selector);

Expand Down Expand Up @@ -177,7 +171,7 @@ public function find($selector, $idx = null)
*
* @return string
*/
public function html()
public function html(): string
{
if ($this::$callback !== null) {
call_user_func($this::$callback, $this);
Expand All @@ -191,7 +185,7 @@ public function html()
*
* @return string
*/
public function innerHtml()
public function innerHtml(): string
{
$text = '';
foreach ($this->document->documentElement->childNodes as $node) {
Expand All @@ -206,7 +200,7 @@ public function innerHtml()
*
* @return string
*/
public function text()
public function text(): string
{
return $this->document->textContent;
}
Expand All @@ -218,7 +212,7 @@ public function text()
*
* @return string
*/
public function save($filepath = '')
public function save(string $filepath = ''): string
{
$string = $this->innerHtml();
if ($filepath !== '') {
Expand All @@ -231,7 +225,7 @@ public function save($filepath = '')
/**
* @param $functionName
*/
public function set_callback($functionName)
public function set_callback($functionName): void
{
$this::$callback = $functionName;
}
Expand All @@ -247,20 +241,17 @@ public function clear()
*/
public function __get($name)
{
switch ($name) {
case 'outertext':
return $this->html();
case 'innertext':
return $this->innerHtml();
case 'plaintext':
return $this->text();
}
return match ($name) {
'outertext' => $this->html(),
'innertext' => $this->innerHtml(),
'plaintext' => $this->text(),
default => null,
};

return null;
}

/**
* @return mixed
* @return string
*/
public function __toString()
{
Expand All @@ -269,11 +260,11 @@ public function __toString()

/**
* @param string $selector
* @param int $idx
* @param int|null $idx
*
* @return Element|NodeList|null
*/
public function __invoke($selector, $idx = null)
public function __invoke(string $selector, int $idx = null): NodeList|Element|null
{
return $this->find($selector, $idx);
}
Expand All @@ -298,7 +289,7 @@ public function __call($name, $arguments)
* @param $name
* @param $arguments
*
* @return bool|Document
* @return Document
*
* @throws BadMethodCallException
*/
Expand Down
Loading