Skip to content

Commit

Permalink
Do not use implicitly nullable parameters (#984)
Browse files Browse the repository at this point in the history
Implicitly nullable parameters will be deprecated in PHP 8.4
(see https://wiki.php.net/rfc/deprecate-implicitly-nullable-types).
To avoid deprecation warnings, replace all implicitly nullable
parameters with explicit ones. Unfortunately, this also means
that we have to drop support for PHP 7.0.
  • Loading branch information
sebastianbergmann committed Mar 2, 2024
1 parent 1bcbb21 commit 051ad21
Show file tree
Hide file tree
Showing 31 changed files with 44 additions and 37 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ on:
pull_request:

jobs:
tests_70:
tests_71:
runs-on: "ubuntu-latest"
name: "PHP 7.0 Unit Tests"
name: "PHP 7.1 Unit Tests"
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "xdebug"
php-version: "7.0"
php-version: "7.1"
tools: composer:v2
- name: "Install dependencies"
run: |
Expand All @@ -34,7 +34,6 @@ jobs:
strategy:
matrix:
php-version:
- "7.1"
- "7.2"
- "7.3"
- "7.4"
Expand Down Expand Up @@ -71,17 +70,17 @@ jobs:
run: "composer update --no-progress --prefer-dist"
- name: "Tests"
run: "test_old/run-php-src.sh 7.3.21"
test_old_80_70:
test_old_80_71:
runs-on: "ubuntu-latest"
name: "PHP 8.1 Code on PHP 7.0 Integration Tests"
name: "PHP 8.1 Code on PHP 7.1 Integration Tests"
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "7.0"
php-version: "7.1"
tools: composer:v2
- name: "Install PHP 8 dependencies"
run: "composer update --no-progress --prefer-dist"
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Version 4.19.0 (2024-MM-DD)
---------------------------

### Changed

* Do not use implicitly nullable parameters
* PHP 7.0 is no longer supported

Version 4.18.0 (2023-12-10)
---------------------------

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=7.0",
"php": ">=7.1",
"ext-tokenizer": "*"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/ConstExprEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ConstExprEvaluator
*
* @param callable|null $fallbackEvaluator To call if subexpression cannot be evaluated
*/
public function __construct(callable $fallbackEvaluator = null) {
public function __construct(?callable $fallbackEvaluator = null) {
$this->fallbackEvaluator = $fallbackEvaluator ?? function(Expr $expr) {
throw new ConstExprEvaluationException(
"Expression of type {$expr->getType()} cannot be evaluated"
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Internal/PrintableNewAnonClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PrintableNewAnonClassNode extends Expr
public $stmts;

public function __construct(
array $attrGroups, int $flags, array $args, Node\Name $extends = null, array $implements,
array $attrGroups, int $flags, array $args, ?Node\Name $extends = null, array $implements,
array $stmts, array $attributes
) {
parent::__construct($attributes);
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(array $options = []) {
* @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
* ErrorHandler\Throwing
*/
public function startLexing(string $code, ErrorHandler $errorHandler = null) {
public function startLexing(string $code, ?ErrorHandler $errorHandler = null) {
if (null === $errorHandler) {
$errorHandler = new ErrorHandler\Throwing();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Lexer/Emulative.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(array $options = [])
}
}

public function startLexing(string $code, ErrorHandler $errorHandler = null) {
public function startLexing(string $code, ?ErrorHandler $errorHandler = null) {
$emulators = array_filter($this->emulators, function($emulator) use($code) {
return $emulator->isEmulationNeeded($code);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(ErrorHandler $errorHandler) {
*
* @param Name|null $namespace Null is the global namespace
*/
public function startNamespace(Name $namespace = null) {
public function startNamespace(?Name $namespace = null) {
$this->namespace = $namespace;
$this->origAliases = $this->aliases = [
Stmt\Use_::TYPE_NORMAL => [],
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Arg.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Arg extends NodeAbstract
*/
public function __construct(
Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [],
Identifier $name = null
?Identifier $name = null
) {
$this->attributes = $attributes;
$this->name = $name;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Expr/ArrayDimFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ArrayDimFetch extends Expr
* @param null|Expr $dim Array index / dim
* @param array $attributes Additional attributes
*/
public function __construct(Expr $var, Expr $dim = null, array $attributes = []) {
public function __construct(Expr $var, ?Expr $dim = null, array $attributes = []) {
$this->attributes = $attributes;
$this->var = $var;
$this->dim = $dim;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Expr/ArrayItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArrayItem extends Expr
* @param bool $byRef Whether to assign by reference
* @param array $attributes Additional attributes
*/
public function __construct(Expr $value, Expr $key = null, bool $byRef = false, array $attributes = [], bool $unpack = false) {
public function __construct(Expr $value, ?Expr $key = null, bool $byRef = false, array $attributes = [], bool $unpack = false) {
$this->attributes = $attributes;
$this->key = $key;
$this->value = $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Expr/Exit_.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Exit_ extends Expr
* @param null|Expr $expr Expression
* @param array $attributes Additional attributes
*/
public function __construct(Expr $expr = null, array $attributes = []) {
public function __construct(?Expr $expr = null, array $attributes = []) {
$this->attributes = $attributes;
$this->expr = $expr;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Expr/Yield_.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Yield_ extends Expr
* @param null|Expr $key Key expression
* @param array $attributes Additional attributes
*/
public function __construct(Expr $value = null, Expr $key = null, array $attributes = []) {
public function __construct(?Expr $value = null, ?Expr $key = null, array $attributes = []) {
$this->attributes = $attributes;
$this->key = $key;
$this->value = $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function __toString() : string {
*
* @return static|null Sliced name
*/
public function slice(int $offset, int $length = null) {
public function slice(int $offset, ?int $length = null) {
$numParts = count($this->parts);

$realOffset = $offset < 0 ? $offset + $numParts : $offset;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Param extends NodeAbstract
* @param AttributeGroup[] $attrGroups PHP attribute groups
*/
public function __construct(
$var, Expr $default = null, $type = null,
$var, ?Expr $default = null, $type = null,
bool $byRef = false, bool $variadic = false,
array $attributes = [],
int $flags = 0,
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Break_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Break_ extends Node\Stmt
* @param null|Node\Expr $num Number of loops to break
* @param array $attributes Additional attributes
*/
public function __construct(Node\Expr $num = null, array $attributes = []) {
public function __construct(?Node\Expr $num = null, array $attributes = []) {
$this->attributes = $attributes;
$this->num = $num;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Catch_.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Catch_ extends Node\Stmt
* @param array $attributes Additional attributes
*/
public function __construct(
array $types, Expr\Variable $var = null, array $stmts = [], array $attributes = []
array $types, ?Expr\Variable $var = null, array $stmts = [], array $attributes = []
) {
$this->attributes = $attributes;
$this->types = $types;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Continue_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Continue_ extends Node\Stmt
* @param null|Node\Expr $num Number of loops to continue
* @param array $attributes Additional attributes
*/
public function __construct(Node\Expr $num = null, array $attributes = []) {
public function __construct(?Node\Expr $num = null, array $attributes = []) {
$this->attributes = $attributes;
$this->num = $num;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Declare_.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Declare_ extends Node\Stmt
* @param Node\Stmt[]|null $stmts Statements
* @param array $attributes Additional attributes
*/
public function __construct(array $declares, array $stmts = null, array $attributes = []) {
public function __construct(array $declares, ?array $stmts = null, array $attributes = []) {
$this->attributes = $attributes;
$this->declares = $declares;
$this->stmts = $stmts;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/EnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EnumCase extends Node\Stmt
* @param AttributeGroup[] $attrGroups PHP attribute groups
* @param array $attributes Additional attributes
*/
public function __construct($name, Node\Expr $expr = null, array $attrGroups = [], array $attributes = []) {
public function __construct($name, ?Node\Expr $expr = null, array $attrGroups = [], array $attributes = []) {
parent::__construct($attributes);
$this->name = \is_string($name) ? new Node\Identifier($name) : $name;
$this->expr = $expr;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Namespace_.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Namespace_ extends Node\Stmt
* @param null|Node\Stmt[] $stmts Statements
* @param array $attributes Additional attributes
*/
public function __construct(Node\Name $name = null, $stmts = [], array $attributes = []) {
public function __construct(?Node\Name $name = null, $stmts = [], array $attributes = []) {
$this->attributes = $attributes;
$this->name = $name;
$this->stmts = $stmts;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/PropertyProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PropertyProperty extends Node\Stmt
* @param null|Node\Expr $default Default value
* @param array $attributes Additional attributes
*/
public function __construct($name, Node\Expr $default = null, array $attributes = []) {
public function __construct($name, ?Node\Expr $default = null, array $attributes = []) {
$this->attributes = $attributes;
$this->name = \is_string($name) ? new Node\VarLikeIdentifier($name) : $name;
$this->default = $default;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Return_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Return_ extends Node\Stmt
* @param null|Node\Expr $expr Expression
* @param array $attributes Additional attributes
*/
public function __construct(Node\Expr $expr = null, array $attributes = []) {
public function __construct(?Node\Expr $expr = null, array $attributes = []) {
$this->attributes = $attributes;
$this->expr = $expr;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/StaticVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StaticVar extends Node\Stmt
* @param array $attributes Additional attributes
*/
public function __construct(
Expr\Variable $var, Node\Expr $default = null, array $attributes = []
Expr\Variable $var, ?Node\Expr $default = null, array $attributes = []
) {
$this->attributes = $attributes;
$this->var = $var;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/TryCatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TryCatch extends Node\Stmt
* @param null|Finally_ $finally Optional finally node
* @param array $attributes Additional attributes
*/
public function __construct(array $stmts, array $catches, Finally_ $finally = null, array $attributes = []) {
public function __construct(array $stmts, array $catches, ?Finally_ $finally = null, array $attributes = []) {
$this->attributes = $attributes;
$this->stmts = $stmts;
$this->catches = $catches;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/NodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(array $options = []) {
*
* @return string Dumped value
*/
public function dump($node, string $code = null) : string {
public function dump($node, ?string $code = null) : string {
$this->code = $code;
return $this->dumpRecursive($node);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NameResolver extends NodeVisitorAbstract
* @param ErrorHandler|null $errorHandler Error handler
* @param array $options Options
*/
public function __construct(ErrorHandler $errorHandler = null, array $options = []) {
public function __construct(?ErrorHandler $errorHandler = null, array $options = []) {
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing);
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
$this->replaceNodes = $options['replaceNodes'] ?? true;
Expand Down Expand Up @@ -164,7 +164,7 @@ public function enterNode(Node $node) {
return null;
}

private function addAlias(Stmt\UseUse $use, int $type, Name $prefix = null) {
private function addAlias(Stmt\UseUse $use, int $type, ?Name $prefix = null) {
// Add prefix for group uses
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
// Type is determined either by individual element or whole use declaration
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface Parser
* @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
* the parser was unable to recover from an error).
*/
public function parse(string $code, ErrorHandler $errorHandler = null);
public function parse(string $code, ?ErrorHandler $errorHandler = null);
}
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(array $parsers) {
$this->parsers = $parsers;
}

public function parse(string $code, ErrorHandler $errorHandler = null) {
public function parse(string $code, ?ErrorHandler $errorHandler = null) {
if (null === $errorHandler) {
$errorHandler = new ErrorHandler\Throwing;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function __construct(Lexer $lexer, array $options = []) {
* @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
* the parser was unable to recover from an error).
*/
public function parse(string $code, ErrorHandler $errorHandler = null) {
public function parse(string $code, ?ErrorHandler $errorHandler = null) {
$this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;

$this->lexer->startLexing($code, $this->errorHandler);
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ParserFactory
*
* @return Parser The parser instance
*/
public function create(int $kind, Lexer $lexer = null, array $parserOptions = []) : Parser {
public function create(int $kind, ?Lexer $lexer = null, array $parserOptions = []) : Parser {
if (null === $lexer) {
$lexer = new Lexer\Emulative();
}
Expand Down

0 comments on commit 051ad21

Please sign in to comment.