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

Add PHP 8.4 compliance for v4 #296

Merged
merged 3 commits into from
Sep 5, 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
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ jobs:
matrix:
php-versions: ['8.0', '8.1', '8.2', '8.3']
coverage: ['pcov']
code-style: ['no']
code-analysis: ['no']
include:
- php-versions: '7.4'
coverage: 'none'
code-style: 'yes'
code-analysis: 'yes'
- php-versions: '8.4'
coverage: 'pcov'
code-style: 'no'
code-analysis: 'yes'
steps:
- name: Checkout
Expand Down Expand Up @@ -48,8 +54,8 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Code Analysis (PHP CS-Fixer)
if: matrix.code-analysis == 'yes'
run: php vendor/bin/php-cs-fixer fix --dry-run --diff
if: matrix.code-style == 'yes'
run: PHP_CS_FIXER_IGNORE_ENV=true php vendor/bin/php-cs-fixer fix --dry-run --diff

- name: Code Analysis (PHPStan)
if: matrix.code-analysis == 'yes'
Expand Down
4 changes: 4 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
$config->setRules([
'@PSR1' => true,
'@Symfony' => true,
'nullable_type_declaration' => [
'syntax' => 'question_mark',
],
'nullable_type_declaration_for_default_null_value' => true,
]);
$config->setFinder($finder);

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
}
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.56",
"phpstan/phpstan": "^1.11",
"friendsofphp/php-cs-fixer": "^3.64",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit" : "^9.6"
},
"scripts": {
Expand All @@ -56,7 +56,7 @@
"phpstan analyse --generate-baseline phpstan-baseline.neon"
],
"cs-fixer": [
"php-cs-fixer fix"
"PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix"
],
"phpunit": [
"phpunit --configuration tests/phpunit.xml"
Expand Down
2 changes: 1 addition & 1 deletion lib/ContextStackTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function popContext(): void
$this->elementMap,
$this->contextUri,
$this->namespaceMap,
$this->classMap
$this->classMap,
) = array_pop($this->contextStack);
}
}
22 changes: 18 additions & 4 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,17 @@ public function getWriter(): Writer
*/
public function parse($input, ?string $contextUri = null, ?string &$rootElementName = null)
{
if (is_resource($input)) {
if (!is_string($input)) {
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
if (is_resource($input)) {
$input = (string) stream_get_contents($input);
} else {
// Input is not a string and not a resource.
// Therefore, it has to be a closed resource.
// Effectively empty input has been passed in.
$input = '';
}
}

// If input is empty, then it's safe to throw an exception
Expand Down Expand Up @@ -153,10 +160,17 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN
*/
public function expect($rootElementName, $input, ?string $contextUri = null)
{
if (is_resource($input)) {
if (!is_string($input)) {
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
if (is_resource($input)) {
$input = (string) stream_get_contents($input);
} else {
// Input is not a string and not a resource.
// Therefore, it has to be a closed resource.
// Effectively empty input has been passed in.
$input = '';
}
}

// If input is empty, then it's safe to throw an exception
Expand Down
2 changes: 1 addition & 1 deletion lib/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function writeAttribute($name, $value): bool

list(
$namespace,
$localName
$localName,
) = Service::parseClarkNotation($name);

if (array_key_exists($namespace, $this->namespaceMap)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ public function providesEmptyInput(): array
$data[] = [$emptyResource];
$data[] = [''];

// Also test trying to parse a resource stream that has already been closed.
$xml = <<<XML
<root xmlns="http://sabre.io/ns">
<child>value</child>
</root>
XML;
$stream = fopen('php://memory', 'r+');
self:assertIsResource($stream);
fwrite($stream, $xml);
rewind($stream);
fclose($stream);
$data[] = [$stream];

return $data;
}

Expand Down