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

Improve PHP 8.4 compatibility #551

Merged
merged 7 commits into from
Oct 17, 2024
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
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.1', '7.4', '7.3', '7.2', '7.1', '7.0', '5.6']
php: ['8.3', '8.1', '8.0', '7.4']
coverage: [false]
random: [false]
include:
- php: '8.0'
- php: '8.2'
coverage: true
- php: '8.0'
- php: '8.2'
random: true
- php: '8.3'
- php: '8.4'
experimental: true

steps:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A collection of AMP tools making it easier to publish and host AMP pages with PHP.",
"license": "Apache-2.0",
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"php": "^7.4 || ^8.0",
"ext-dom": "*",
"ext-filter": "*",
"ext-iconv": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Executable.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ abstract class Executable
* @param Colors|null $colors Optional. Instance of the Colors object to use. Defaults to null to instantiate a
* new one.
*/
public function __construct($autocatch = true, Options $options = null, Colors $colors = null)
public function __construct($autocatch = true, ?Options $options = null, ?Colors $colors = null)
{
if ($autocatch) {
set_exception_handler([$this, 'fatal']);
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
* @param Colors $colors Optional. Configured color object.
* @throws InvalidArgument When arguments can't be read.
*/
public function __construct(Colors $colors = null)
public function __construct(?Colors $colors = null)
{
$this->colors = $colors instanceof Colors ? $colors : new Colors();

Expand Down Expand Up @@ -542,7 +542,7 @@
}

if (
is_array($_SERVER)

Check failure on line 545 in src/Cli/Options.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Call to function is_array() with array will always evaluate to true.
&&
array_key_exists('argv', $_SERVER)
&&
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/TableFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
*
* @param Colors|null $colors Optional. Instance of the Colors helper object.
*/
public function __construct(Colors $colors = null)
public function __construct(?Colors $colors = null)
{
// Try to get terminal width.
$width = $this->getTerminalWidth();
Expand Down Expand Up @@ -381,11 +381,11 @@
*/
protected function wordwrap($string, $width = 75, $break = "\n", $cut = false)
{
if (! is_int($width) || $width < 0) {

Check failure on line 384 in src/Cli/TableFormatter.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Call to function is_int() with int will always evaluate to true.
$width = 75;
}

if (! is_string($break) || empty($break)) {

Check failure on line 388 in src/Cli/TableFormatter.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Call to function is_string() with string will always evaluate to true.
$break = "\n";
}

Expand Down
2 changes: 1 addition & 1 deletion src/CssLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
$pattern = '/^(?<numeral>\d+(?:\.\d+)?)(?<unit>px|em|rem|vh|vw|vmin|vmax)?$/';
if (preg_match($pattern, $this->attrValue, $match)) {
$this->isValid = true;
$this->numeral = isset($match['numeral']) ? (float)$match['numeral'] : $this->numeral;

Check failure on line 114 in src/CssLength.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Offset 'numeral' on array{0: string, numeral: non-empty-string, 1: non-empty-string, unit?: 'em'|'px'|'rem'|'vh'|'vmax'|'vmin'|'vw', 2?: 'em'|'px'|'rem'|'vh'|'vmax'|'vmin'|'vw'} in isset() always exists and is not nullable.
$this->unit = isset($match['unit']) ? $match['unit'] : $this->unit;
$this->unit = $match['unit'] ?? $this->unit;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
$options = [Option::ENCODING => $options];
}

$encoding = isset($options[Option::ENCODING]) ? $options[Option::ENCODING] : null;
$encoding = $options[ Option::ENCODING ] ?? null;

$dom = new self('', $encoding);

Expand Down Expand Up @@ -258,7 +258,7 @@
$options = [Option::ENCODING => $options];
}

$encoding = isset($options[Option::ENCODING]) ? $options[Option::ENCODING] : null;
$encoding = $options[ Option::ENCODING ] ?? null;

$dom = new self('', $encoding);

Expand Down Expand Up @@ -380,7 +380,7 @@
continue;
}

if (! $filter instanceof Filter) {

Check failure on line 383 in src/Dom/Document.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Instanceof between AmpProject\Dom\Document\Filter and AmpProject\Dom\Document\Filter will always evaluate to true.
throw InvalidDocumentFilter::forFilter($filter);
}

Expand Down Expand Up @@ -413,7 +413,7 @@
* @return string The HTML, or false if an error occurred.
*/
#[\ReturnTypeWillChange]
public function saveHTML(DOMNode $node = null)
public function saveHTML(?DOMNode $node = null)
{
return $this->saveHTMLFragment($node);
}
Expand All @@ -424,7 +424,7 @@
* @param DOMNode|null $node Optional. Parameter to output a subset of the document.
* @return string The HTML fragment, or false if an error occurred.
*/
public function saveHTMLFragment(DOMNode $node = null)
public function saveHTMLFragment(?DOMNode $node = null)
{
$filtersInReverse = array_reverse($this->filters);

Expand Down Expand Up @@ -551,7 +551,7 @@
$content = preg_replace(self::HTML_STRUCTURE_HTML_START_TAG, '', $content, 1);

preg_match(self::HTML_STRUCTURE_HTML_END_TAG, $content, $matches);
$htmlEnd = isset($matches['html_end']) ? $matches['html_end'] : $htmlEnd;
$htmlEnd = $matches['html_end'] ?? $htmlEnd;
$content = preg_replace(self::HTML_STRUCTURE_HTML_END_TAG, '', $content, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dom/Document/Filter/AmpBindAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function afterSave($html)
||
in_array($attrName, $this->convertedAmpBindAttributes, true)
) {
$attrValue = isset($attrMatches['value']) ? $attrMatches['value'] : '=""';
$attrValue = $attrMatches['value'] ?? '=""';
$newAttrs .= " [{$attrName}]{$attrValue}";
} else {
$newAttrs .= $attrMatches[0];
Expand Down
2 changes: 1 addition & 1 deletion src/Dom/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function offsetUnset($option)
#[\ReturnTypeWillChange]
public function offsetGet($option)
{
return isset($this->options[$option]) ? $this->options[$option] : null;
return $this->options[ $option ] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Optimizer/Error/CannotPreloadImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class CannotPreloadImage implements Error
* @param Element|null $element Optional. Image element that has the srcset attribute, or null if no element.
* @return self
*/
public static function fromImageWithSrcsetAttribute(Element $element = null)
public static function fromImageWithSrcsetAttribute(?Element $element = null)
{
$message = self::SRCSET_STRING;

Expand Down
10 changes: 5 additions & 5 deletions src/Optimizer/TransformationEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ final class TransformationEngine
* @param Configuration|null $configuration Optional. Configuration data to use for setting up the transformers.
* @param RemoteGetRequest|null $remoteRequest Optional. Transport to use for remote requests. Defaults to the
* CurlRemoteGetRequest implementation shipped with the library.
* @param Spec $spec Optional. Validator spec instance to use.
* @param Spec|null $spec Optional. Validator spec instance to use.
*/
public function __construct(
Configuration $configuration = null,
RemoteGetRequest $remoteRequest = null,
Spec $spec = null
?Configuration $configuration = null,
?RemoteGetRequest $remoteRequest = null,
?Spec $spec = null
) {
$this->configuration = isset($configuration) ? $configuration : new DefaultConfiguration();
$this->configuration = $configuration ?? new DefaultConfiguration();
$this->remoteRequest = $remoteRequest;
$this->spec = $spec;

Expand Down
2 changes: 1 addition & 1 deletion src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ final class Url
* @param Url|null $baseUrl Base URL.
* @throws FailedToParseUrl Exception when the URL or Base URL is malformed.
*/
public function __construct($url = null, Url $baseUrl = null)
public function __construct($url = null, ?Url $baseUrl = null)
{
$parsedUrl = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Validator/ValidationHandler.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Validator/ValidationResult.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Validator/ValidatorRules.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/Optimizer/TransformationEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testDependencyResolution($transformerClass)
* @param Configuration|null $configuration Optional. Configuration object to use.
* @return TransformationEngine Transformation engine instance to test against.
*/
private function getTransformationEngine(Configuration $configuration = null)
private function getTransformationEngine(?Configuration $configuration = null)
{
return new TransformationEngine(
$configuration,
Expand Down
Loading