Skip to content

Commit

Permalink
Fix dynamic return type extension for term_exists() (szepeviktor#153)
Browse files Browse the repository at this point in the history
* Fix return type extension for term_exists()

* Reduce nesting depth

* Update src/TermExistsDynamicFunctionReturnTypeExtension.php

---------

Co-authored-by: Viktor Szépe <[email protected]>
  • Loading branch information
IanDelMar and szepeviktor authored Feb 25, 2023
1 parent e1c5e57 commit c67f764
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
37 changes: 18 additions & 19 deletions src/TermExistsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$termType = $scope->getType($args[0]->value);
$taxonomyType = isset($args[1]) ? $scope->getType($args[1]->value) : new ConstantStringType('');

if ($termType instanceof NullType) {
$returnType = [new NullType()];

if ($termType->isNull()->yes()) {
return new NullType();
}

if (($termType instanceof ConstantIntegerType) && $termType->getValue() === 0) {
return new ConstantIntegerType(0);
if (($termType instanceof ConstantIntegerType)) {
if ($termType->getValue() === 0) {
return new ConstantIntegerType(0);
}
} elseif ($termType->isInteger()->no() === false) {
$returnType[] = new ConstantIntegerType(0);
}

$withTaxonomy = new ConstantArrayType(
Expand All @@ -71,24 +77,17 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
);
$withoutTaxonomy = new StringType();

if (($taxonomyType instanceof ConstantStringType) && $taxonomyType->getValue() !== '') {
return TypeCombinator::union(
$withTaxonomy,
new NullType()
);
if (count($taxonomyType->getConstantStrings()) === 0) {
$returnType[] = $withTaxonomy;
$returnType[] = $withoutTaxonomy;
return TypeCombinator::union(...$returnType);
}

if (($taxonomyType instanceof ConstantStringType) && $taxonomyType->getValue() === '') {
return TypeCombinator::union(
$withoutTaxonomy,
new NullType()
);
foreach ($taxonomyType->getConstantStrings() as $constantString) {
$returnType[] = $constantString->getValue() === ''
? $withoutTaxonomy
: $withTaxonomy;
}

return TypeCombinator::union(
$withTaxonomy,
$withoutTaxonomy,
new NullType()
);
return TypeCombinator::union(...$returnType);
}
}
7 changes: 4 additions & 3 deletions tests/data/term_exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
// Empty taxonomy
assertType('string|null', term_exists(123));
assertType('string|null', term_exists(123, ''));
assertType('string|null', term_exists($term));
assertType('string|null', term_exists($term, ''));
assertType('0|string|null', term_exists($term));
assertType('0|string|null', term_exists($term, ''));

// Fixed taxonomy string
assertType('array{term_id: string, term_taxonomy_id: string}|null', term_exists(123, 'category'));
assertType('array{term_id: string, term_taxonomy_id: string}|null', term_exists($term, 'category'));
assertType('0|array{term_id: string, term_taxonomy_id: string}|null', term_exists($term, 'category'));

// Unknown taxonomy type
assertType('array{term_id: string, term_taxonomy_id: string}|string|null', term_exists(123, $taxo));
assertType('0|array{term_id: string, term_taxonomy_id: string}|string|null', term_exists($term, $taxo));

// Term 0
assertType('0', term_exists(0));
Expand Down

0 comments on commit c67f764

Please sign in to comment.