Skip to content

Commit

Permalink
add more testcases and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
verfriemelt-dot-org committed Dec 14, 2022
1 parent 04b11e0 commit 56c47c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
42 changes: 22 additions & 20 deletions src/Type/Php/DateIntervalDynamicReturnTypeExtension.php
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare( strict_types = 1 );

namespace PHPStan\Type\Php;

Expand All @@ -13,41 +13,43 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;

class DateIntervalDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
class DateIntervalDynamicReturnTypeExtension
implements DynamicStaticMethodReturnTypeExtension
{

public function getClass(): string
{
return DateInterval::class;
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
public function isStaticMethodSupported( MethodReflection $methodReflection ): bool
{
return $methodReflection->getName() === 'createFromDateString';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
public function getTypeFromStaticMethodCall( MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope ): ?Type
{
$strings = $scope->getType($methodCall->getArgs()[0]->value)->getConstantStrings();
if ($strings === []) {
return null;
$strings = $scope->getType( $methodCall->getArgs()[ 0 ]->value )->getConstantStrings();

if ( $strings === [] ) {
return null;
}

if (count($strings) === 1) {
if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) {
return new ConstantBooleanType(false);
}
return new ObjectType(DateInterval::class);

if ( count( $strings ) === 1 ) {
if ( DateInterval::createFromDateString( $strings[ 0 ]->getValue() ) === false ) {
return new ConstantBooleanType( false );
}

return new ObjectType( DateInterval::class );
}
foreach($strings as $string) {
if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) {
return null;
}

foreach ( $strings as $string ) {
if ( DateInterval::createFromDateString( $string->getValue() ) === false ) {
return null;
}
}

return new ObjectType(DateInterval::class);
return new ObjectType( DateInterval::class );
}

}
12 changes: 10 additions & 2 deletions tests/PHPStan/Analyser/data/bug-8442.php
Expand Up @@ -10,11 +10,19 @@ function () {
assertType('DateInterval', DateInterval::createFromDateString('1 Day'));

if (rand(0,1)) {
$interval = 'P1Y';
$interval = '1 day';
} else {
$interval = 'P2Y';
$interval = '2 day';
}

assertType('DateInterval', DateInterval::createFromDateString($interval));

if (rand(0,1)) {
$interval = 'foo';
} else {
$interval = '2 day';
}

assertType('DateInterval|false', DateInterval::createFromDateString($interval));
};

0 comments on commit 56c47c2

Please sign in to comment.