forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,425 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Psalm; | ||
|
||
use Psalm\Internal\Type\TemplateInferredTypeReplacer; | ||
use Psalm\Internal\Type\TemplateResult; | ||
use Psalm\Internal\Type\TemplateStandinTypeReplacer; | ||
use Psalm\Type\Union; | ||
|
||
final class ContextualTypeResolver | ||
{ | ||
private Union $contextual_type; | ||
private TemplateResult $template_result; | ||
private Codebase $codebase; | ||
|
||
public function __construct( | ||
Union $contextual_type, | ||
TemplateResult $template_result, | ||
Codebase $codebase | ||
) { | ||
$this->contextual_type = $contextual_type; | ||
$this->template_result = $template_result; | ||
$this->codebase = $codebase; | ||
} | ||
|
||
public function getCodebase(): Codebase | ||
{ | ||
return $this->codebase; | ||
} | ||
|
||
/** | ||
* @return ($type is Union ? self : null) | ||
*/ | ||
public function withContextualType(?Union $type): ?self | ||
{ | ||
return $type !== null | ||
? new self($type, $this->template_result, $this->codebase) | ||
: null; | ||
} | ||
|
||
public function resolve(): Union | ||
{ | ||
return TemplateInferredTypeReplacer::replace( | ||
$this->contextual_type, | ||
$this->template_result, | ||
$this->codebase, | ||
); | ||
} | ||
|
||
public function fillTemplateResult(Union $input_type): void | ||
{ | ||
TemplateStandinTypeReplacer::fillTemplateResult( | ||
$this->contextual_type, | ||
$this->template_result, | ||
$this->codebase, | ||
null, | ||
$input_type, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Psalm/Internal/Analyzer/ClosureAnalyzerContextualTypeExtractor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\Analyzer; | ||
|
||
use Psalm\ContextualTypeResolver; | ||
use Psalm\Type\Atomic; | ||
use Psalm\Type\Atomic\TCallable; | ||
use Psalm\Type\Atomic\TClosure; | ||
|
||
use function count; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ClosureAnalyzerContextualTypeExtractor | ||
{ | ||
/** | ||
* @return null|TClosure|TCallable | ||
*/ | ||
public static function extract(ContextualTypeResolver $contextual_type_resolver): ?Atomic | ||
{ | ||
$candidates = []; | ||
|
||
$atomics = $contextual_type_resolver | ||
->resolve() | ||
->getAtomicTypes(); | ||
|
||
foreach ($atomics as $atomic) { | ||
if ($atomic instanceof TClosure || $atomic instanceof TCallable) { | ||
$candidates[] = $atomic; | ||
} | ||
} | ||
|
||
return count($candidates) === 1 ? $candidates[0] : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.