示例#1
0
 public function testPrettyPrintExpr()
 {
     $prettyPrinter = new Standard();
     $expr = new Expr\BinaryOp\Mul(new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')), new Expr\Variable('c'));
     $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
     $expr = new Expr\Closure(array('stmts' => array(new Stmt\Return_(new String_("a\nb")))));
     $this->assertEquals("function () {\n    return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
 }
示例#2
0
 public function specifyTypesInCondition(SpecifiedTypes $types, Scope $scope, Node $expr, bool $negated = false, int $source = self::SOURCE_UNKNOWN) : SpecifiedTypes
 {
     if ($expr instanceof Instanceof_ && $expr->class instanceof Name) {
         $class = (string) $expr->class;
         if ($class === 'static') {
             return $types;
         }
         if ($class === 'self' && $scope->getClass() !== null) {
             $class = $scope->getClass();
         }
         $printedExpr = $this->printer->prettyPrintExpr($expr->expr);
         $objectType = new ObjectType($class, false);
         if ($negated) {
             if ($source === self::SOURCE_FROM_AND) {
                 return $types;
             }
             return $types->addSureNotType($expr->expr, $printedExpr, $objectType);
         }
         return $types->addSureType($expr->expr, $printedExpr, $objectType);
     } elseif ($expr instanceof FuncCall && $expr->name instanceof Name && isset($expr->args[0])) {
         $functionName = (string) $expr->name;
         $argumentExpression = $expr->args[0]->value;
         $specifiedType = null;
         if (in_array($functionName, ['is_int', 'is_integer', 'is_long'], true)) {
             $specifiedType = new IntegerType(false);
         } elseif (in_array($functionName, ['is_float', 'is_double', 'is_real'], true)) {
             $specifiedType = new FloatType(false);
         } elseif ($functionName === 'is_null') {
             $specifiedType = new NullType();
         } elseif ($functionName === 'is_array') {
             $specifiedType = new ArrayType(new MixedType(true), false);
         } elseif ($functionName === 'is_bool') {
             $specifiedType = new BooleanType(false);
         } elseif ($functionName === 'is_callable') {
             $specifiedType = new CallableType(false);
         } elseif ($functionName === 'is_resource') {
             $specifiedType = new ResourceType(false);
         } elseif ($functionName === 'is_iterable') {
             $specifiedType = new IterableIterableType(new MixedType(true), false);
         }
         if ($specifiedType !== null) {
             $printedExpr = $this->printer->prettyPrintExpr($argumentExpression);
             if ($negated) {
                 return $types->addSureNotType($argumentExpression, $printedExpr, $specifiedType);
             }
             return $types->addSureType($argumentExpression, $printedExpr, $specifiedType);
         }
     } elseif ($expr instanceof BooleanAnd) {
         if ($source !== self::SOURCE_UNKNOWN && $source !== self::SOURCE_FROM_AND) {
             return $types;
         }
         $types = $this->specifyTypesInCondition($types, $scope, $expr->left, $negated, self::SOURCE_FROM_AND);
         $types = $this->specifyTypesInCondition($types, $scope, $expr->right, $negated, self::SOURCE_FROM_AND);
     } elseif ($expr instanceof BooleanOr) {
         if ($negated) {
             return $types;
         }
         $types = $this->specifyTypesInCondition($types, $scope, $expr->left, $negated, self::SOURCE_FROM_OR);
         $types = $this->specifyTypesInCondition($types, $scope, $expr->right, $negated, self::SOURCE_FROM_OR);
     } elseif ($expr instanceof Node\Expr\BooleanNot) {
         if ($source === self::SOURCE_FROM_AND) {
             return $types;
         }
         $types = $this->specifyTypesInCondition($types, $scope, $expr->expr, !$negated, $source);
     }
     return $types;
 }
示例#3
0
 /** @dataProvider provideTestUnnaturalLiterals */
 public function testUnnaturalLiterals($node, $expected) {
     $prttyPrinter = new PrettyPrinter\Standard;
     $result = $prttyPrinter->prettyPrintExpr($node);
     $this->assertSame($expected, $result);
 }
示例#4
0
 public function testArraySyntaxDefault() {
     $prettyPrinter = new Standard(['shortArraySyntax' => true]);
     $expr = new Expr\Array_([
         new Expr\ArrayItem(new String_('val'), new String_('key'))
     ]);
     $expected = "['key' => 'val']";
     $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
 }
 /**
  * @dataProvider provideTestKindAttributes
  */
 public function testKindAttributes($node, $expected)
 {
     $prttyPrinter = new PrettyPrinter\Standard();
     $result = $prttyPrinter->prettyPrintExpr($node);
     $this->assertSame($expected, $result);
 }
示例#6
0
文件: Scope.php 项目: phpstan/phpstan
 public function specifyFetchedPropertyFromIsset(PropertyFetch $expr) : self
 {
     $exprString = $this->printer->prettyPrintExpr($expr);
     return $this->addMoreSpecificTypes([$exprString => new MixedType(false)]);
 }