Skip to content

Commit

Permalink
Fix wrong and inconsistent PHP native types in atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed May 5, 2024
1 parent 0294324 commit 4b855a8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
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
&& $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

0 comments on commit 4b855a8

Please sign in to comment.