This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Analysis\TolerantExpressionAnalyser and tests
- Loading branch information
Showing
5 changed files
with
100 additions
and
3 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,32 @@ | ||
<?php | ||
|
||
namespace Pinq\Analysis; | ||
|
||
use Pinq\Expressions as O; | ||
|
||
/** | ||
* Implementation of the expression type analyser that will | ||
* convert exceptions from not being able to be statically analysed | ||
* into the mixed type. | ||
* | ||
* @author Elliot Levin <elliotlevin@hotmail.com> | ||
*/ | ||
class TolerantExpressionAnalyser extends ExpressionAnalyser | ||
{ | ||
protected function doWalk(O\Expression $expression) | ||
{ | ||
$caught = false; | ||
try { | ||
return parent::doWalk($expression); | ||
} catch (TypeException $exception) { | ||
$caught = true; | ||
} catch (\ReflectionException $exception) { | ||
$caught = true; | ||
} | ||
|
||
if($caught) { | ||
$this->analysis[$expression] = $this->typeSystem->getNativeType(INativeType::TYPE_MIXED); | ||
return $expression; | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
Tests/Integration/Analysis/TolerantExpressionAnalysisTest.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,49 @@ | ||
<?php | ||
|
||
namespace Pinq\Tests\Integration\Analysis; | ||
|
||
use Pinq\Analysis\INativeType; | ||
use Pinq\Analysis\TolerantExpressionAnalyser; | ||
use Pinq\Expressions as O; | ||
|
||
/** | ||
* @author Elliot Levin <elliotlevin@hotmail.com> | ||
*/ | ||
class WeakExpressionAnalysisTest extends ExpressionAnalysisTestCase | ||
{ | ||
protected function setUpExpressionAnalyser() | ||
{ | ||
return new TolerantExpressionAnalyser($this->typeSystem); | ||
} | ||
|
||
public function testThatUnknownVariableReturnsMixed() | ||
{ | ||
$this->assertReturnsNativeType(function () { $var; }, INativeType::TYPE_MIXED); | ||
} | ||
|
||
public function testThatDynamicExpressionsReturnsMixed() | ||
{ | ||
$this->assertReturnsNativeType(function () { $class::$field; }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { $class::method(); }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { Foo::$method(); }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { Foo::$$field; }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { $this->$foo; }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { $this->field; }, INativeType::TYPE_MIXED); | ||
$this->assertReturnsNativeType(function () { $this->field[434]->{__NON_CONSTANT}->abc()[self::NON_CONSTANT]; }, INativeType::TYPE_MIXED); | ||
} | ||
|
||
public function testThatAllUnknownExpressionsAreMixedInTheTypeAnalysis() | ||
{ | ||
$analysis = $this->getAnalysis(function () { | ||
$this->field[434]->abc(); | ||
}); | ||
|
||
/** @var O\VariableExpression $variable */ | ||
$variable = $analysis | ||
->getExpression() | ||
->getValue() | ||
->getValue(); | ||
|
||
$this->assertSame(INativeType::TYPE_MIXED, $analysis->getReturnTypeOf($variable)->getIdentifier()); | ||
} | ||
} |