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

Fix wrong and inconsistent PHP native types in atomic #10960

Open
wants to merge 1 commit into
base: 5.x
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
27 changes: 25 additions & 2 deletions src/Psalm/Type/Atomic/TFalse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,39 @@
*/
final class TFalse extends TBool
{
/** @var false */
/**
* @readonly
* @var false
*/
public $value = false;

public function getKey(bool $include_extra = true): string
{
return 'false';
}

/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): ?string {
if ($analysis_php_version_id >= 8_02_00) {
return $this->getKey();
}

if ($analysis_php_version_id >= 7_00_00) {
return 'bool';
}

return null;
}

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
return $analysis_php_version_id >= 8_02_00;
}
}
4 changes: 3 additions & 1 deletion src/Psalm/Type/Atomic/TIterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function toPhpString(

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return $this->type_params[0]->isMixed() && $this->type_params[1]->isMixed();
return $analysis_php_version_id >= 7_01_00
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does 7.1 actually should be supported?

&& $this->type_params[0]->isMixed()
&& $this->type_params[1]->isMixed();
}

public function equals(Atomic $other_type, bool $ensure_source_equality): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Type/Atomic/TNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public function toPhpString(
?string $this_class,
int $analysis_php_version_id
): ?string {
return null;
return $analysis_php_version_id >= 8_02_00 ? $this->getKey() : null;
}

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
return $analysis_php_version_id >= 8_02_00;
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function toPhpString(

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return true;
return $analysis_php_version_id >= 7_02_00;
}
}
27 changes: 25 additions & 2 deletions src/Psalm/Type/Atomic/TTrue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,39 @@
*/
final class TTrue extends TBool
{
/** @var true */
/**
* @readonly
* @var true
*/
public $value = true;

public function getKey(bool $include_extra = true): string
{
return 'true';
}

/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): ?string {
if ($analysis_php_version_id >= 8_02_00) {
return $this->getKey();
}

if ($analysis_php_version_id >= 7_00_00) {
return 'bool';
}

return null;
}

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
return $analysis_php_version_id >= 8_02_00;
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TVoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function toPhpString(

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return true;
return $analysis_php_version_id >= 7_01_00;
}
}
21 changes: 21 additions & 0 deletions tests/FileManipulation/MissingReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,27 @@ function a(): ?bool {
'issues_to_fix' => ['MissingReturnType'],
'safe_types' => false,
],
'returnNullAndReturnTrueNative' => [
'input' => '<?php
function a() {
if (rand(0,1)){
return true;
}

return null;
}',
'output' => '<?php
function a(): true|null {
if (rand(0,1)){
return true;
}

return null;
}',
'php_version' => '8.2',
'issues_to_fix' => ['MissingReturnType'],
'safe_types' => false,
],
'staticReturn5.6' => [
'input' => '<?php
class HelloWorld
Expand Down
Loading