Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / FunctionReturnInWriteContextPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\CodeCleaner;
13
14 use PhpParser\Node;
15 use PhpParser\Node\Expr\Array_;
16 use PhpParser\Node\Expr\Assign;
17 use PhpParser\Node\Expr\Empty_;
18 use PhpParser\Node\Expr\FuncCall;
19 use PhpParser\Node\Expr\Isset_;
20 use PhpParser\Node\Expr\MethodCall;
21 use PhpParser\Node\Expr\StaticCall;
22 use Psy\Exception\FatalErrorException;
23
24 /**
25  * Validate that the functions are used correctly.
26  *
27  * @author Martin HasoĊˆ <martin.hason@gmail.com>
28  */
29 class FunctionReturnInWriteContextPass extends CodeCleanerPass
30 {
31     const PHP55_MESSAGE = 'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)';
32     const EXCEPTION_MESSAGE = "Can't use function return value in write context";
33
34     private $isPhp55;
35
36     public function __construct()
37     {
38         $this->isPhp55 = version_compare(PHP_VERSION, '5.5', '>=');
39     }
40
41     /**
42      * Validate that the functions are used correctly.
43      *
44      * @throws FatalErrorException if a function is passed as an argument reference
45      * @throws FatalErrorException if a function is used as an argument in the isset
46      * @throws FatalErrorException if a function is used as an argument in the empty, only for PHP < 5.5
47      * @throws FatalErrorException if a value is assigned to a function
48      *
49      * @param Node $node
50      */
51     public function enterNode(Node $node)
52     {
53         if ($node instanceof Array_ || $this->isCallNode($node)) {
54             $items = $node instanceof Array_ ? $node->items : $node->args;
55             foreach ($items as $item) {
56                 if ($item->byRef && $this->isCallNode($item->value)) {
57                     throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
58                 }
59             }
60         } elseif ($node instanceof Isset_) {
61             foreach ($node->vars as $var) {
62                 if (!$this->isCallNode($var)) {
63                     continue;
64                 }
65
66                 $msg = $this->isPhp55 ? self::PHP55_MESSAGE : self::EXCEPTION_MESSAGE;
67                 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
68             }
69         } elseif ($node instanceof Empty_ && !$this->isPhp55 && $this->isCallNode($node->expr)) {
70             throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
71         } elseif ($node instanceof Assign && $this->isCallNode($node->var)) {
72             throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
73         }
74     }
75
76     private function isCallNode(Node $node)
77     {
78         return $node instanceof FuncCall || $node instanceof MethodCall || $node instanceof StaticCall;
79     }
80 }