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

Are changes to the constructor really BC breaks? #497

Open
ondrejmirtes opened this issue Apr 2, 2022 · 4 comments
Open

Are changes to the constructor really BC breaks? #497

ondrejmirtes opened this issue Apr 2, 2022 · 4 comments

Comments

@ondrejmirtes
Copy link

Hi, I've got these messages:

[BC] CHANGED: Default parameter value for parameter $nextAutoIndex of PHPStan\Type\Constant\ConstantArrayType#__construct() changed from 0 to array (
  0 => 0,
)
[BC] CHANGED: The parameter $nextAutoIndex of PHPStan\Type\Constant\ConstantArrayType#__construct() changed from int to a non-contravariant int|array
[BC] CHANGED: The parameter $nextAutoIndex of PHPStan\Type\Constant\ConstantArrayType#__construct() changed from int to int|array

This is how the PHP diff looks like:

 	/**
 	 * @api
 	 * @param array<int, ConstantIntegerType|ConstantStringType> $keyTypes
 	 * @param array<int, Type> $valueTypes
+	 * @param non-empty-list<int>|int $nextAutoIndexes
 	 * @param int[] $optionalKeys
 	 */
 	public function __construct(
 		private array $keyTypes,
 		private array $valueTypes,
-		private int $nextAutoIndex = 0,
+		int|array $nextAutoIndexes = [0],
 		private array $optionalKeys = [],
 	)
 	{
 		assert(count($keyTypes) === count($valueTypes));
 
+		if (is_int($nextAutoIndexes)) {
+			$nextAutoIndexes = [$nextAutoIndexes];
+		}
+
+		$this->nextAutoIndexes = $nextAutoIndexes;
+

I'm widening the type of the parameter, and if the argument is omitted from the call, or if the old type is passed to the parameter, the result is the same.

I don't see how this is a BC break, but I'm open to be corrected :) Thanks.

@Ocramius
Copy link
Member

Ocramius commented Apr 2, 2022

Probably need an exclusion for constructors, since in PHP they can be overridden

@Ocramius Ocramius added the bug label Apr 2, 2022
@Ocramius Ocramius modified the milestones: 7.1.0, 7.0.1 Apr 2, 2022
@ondrejmirtes
Copy link
Author

Only abstract ones and those coming from interfaces can't :)

@Ocramius
Copy link
Member

Ocramius commented Apr 2, 2022

Actually, default value changes are most likely still a BC break, can't remember context though 🤔

As for type, it seems like a bug to me

@Ocramius
Copy link
Member

Ocramius commented Apr 2, 2022

Ah, brain fart: of course child classes cannot reduce the type of a parameter, so that's what is tripping this analysis.

We really just need to exclude constructors, like we do exclude methods for final classes, because the BC break is valid for inheritance, just not for __construct()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants