Skip to content

Commit c1f1dfc

Browse files
authored
Merge pull request #126 from iMattPro/fix-125
Fix issues with PHPParser in PHP 8
2 parents ae4acf1 + 3a4f138 commit c1f1dfc

File tree

2 files changed

+79
-23
lines changed

2 files changed

+79
-23
lines changed

src/Tests/Tests/epv_test_validate_php_functions.php

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
namespace Phpbb\Epv\Tests\Tests;
1111

12-
1312
use Phpbb\Epv\Files\FileInterface;
1413
use Phpbb\Epv\Files\Type\LangFileInterface;
1514
use Phpbb\Epv\Files\Type\PHPFileInterface;
@@ -29,6 +28,7 @@
2928
use PhpParser\Node\Expr\Print_;
3029
use PhpParser\Node\Expr\PropertyFetch;
3130
use PhpParser\Node\Expr\Variable;
31+
use PhpParser\Node\Name;
3232
use PhpParser\Node\Scalar\String_;
3333
use PhpParser\Node\Stmt\Class_;
3434
use PhpParser\Node\Stmt\Declare_;
@@ -333,7 +333,11 @@ private function checkInDefined(If_ $node)
333333
{
334334
$cond = $node->cond;
335335

336-
if ($cond instanceof BooleanNot && $cond->expr instanceof FuncCall && $cond->expr->name->parts[0] == 'defined' && $cond->expr->args[0]->value->value == 'IN_PHPBB')
336+
if ($cond instanceof BooleanNot
337+
&& $cond->expr instanceof FuncCall
338+
&& $cond->expr->name->getFirst() === 'defined'
339+
&& $cond->expr->args[0]->value->value === 'IN_PHPBB'
340+
)
337341
{
338342

339343
if ($node->stmts[0]->expr instanceof Node\Expr\Exit_)
@@ -369,9 +373,9 @@ private function validateFunctionNames(Node $node)
369373
{
370374
$name = $this->getMethodName($node);
371375
}
372-
else if (isset($node->expr) && $node->expr instanceof FuncCall && isset($node->expr->name->parts) && is_array($node->expr->name->parts))
376+
else if (isset($node->expr) && $node->expr instanceof FuncCall && $node->expr->name instanceof Name)
373377
{
374-
$name = (string)$node->expr->name->parts[0];
378+
$name = $node->expr->name->getFirst();
375379
}
376380

377381
if ($name !== null)
@@ -393,9 +397,17 @@ private function validateMethodCalls(Node $node) {
393397
{
394398
$name = $this->getMethodName($node);
395399
}
396-
else if (isset($node->expr) && $node->expr instanceof Node\Expr\MethodCall && !($node->expr->name instanceof Variable) && !($node->expr->name instanceof PropertyFetch) && !($node->expr->name instanceof Concat))
400+
else if (isset($node->expr)
401+
&& $node->expr instanceof Node\Expr\MethodCall
402+
&& !($node->expr->name instanceof Variable
403+
|| $node->expr->name instanceof PropertyFetch
404+
|| $node->expr->name instanceof Concat
405+
|| $node->expr->name instanceof Node\Scalar\Encapsed
406+
|| $node->expr->name instanceof Node\Expr\Ternary
407+
)
408+
)
397409
{
398-
$name = (string)$node->expr->name;
410+
$name = $node->expr->name->toString();
399411
}
400412

401413
if ($name !== null)
@@ -414,21 +426,62 @@ private function getMethodName(Node $node)
414426
{
415427
return null; // This is a variable. We are going to ignore this. We do not want to track variable contents
416428
}
417-
else if ($node->name instanceof Concat)
418-
{
419-
// Only test if both are a string
420-
// This mean that if a user works around this test he can do so, but otherwise we will
421-
// need to parse variables and stuff.
422-
if ($node->name->left instanceof String_ && $node->name->right instanceof String_)
423-
{
424-
return $node->name->left->value . $node->name->right->value;
425-
}
426-
}
427-
else
428-
{
429-
return (string)$node->name;
430-
}
431-
return null;
429+
430+
if ($node->name instanceof Concat)
431+
{
432+
// Only test if both are a string
433+
// This mean that if a user works around this test he can do so, but otherwise we will
434+
// need to parse variables and stuff.
435+
if ($node->name->left instanceof String_ && $node->name->right instanceof String_)
436+
{
437+
return $node->name->left->value . $node->name->right->value;
438+
}
439+
}
440+
else if ($node->name instanceof Expr\Ternary)
441+
{
442+
return $node->name->if;
443+
}
444+
else if ($node->name instanceof Node\Scalar\Encapsed)
445+
{
446+
$encapsed = '';
447+
foreach ($node->name->parts as $part)
448+
{
449+
if ($part instanceof Node\Scalar\EncapsedStringPart)
450+
{
451+
$encapsed .= $part->value;
452+
}
453+
else if ($part instanceof Variable)
454+
{
455+
$encapsed .= $part->name;
456+
}
457+
else if ($part instanceof PropertyFetch)
458+
{
459+
$encapsed .= $part->name->name;
460+
}
461+
else if ($part instanceof ArrayDimFetch)
462+
{
463+
$encapsed .= $part->var->name;
464+
}
465+
else
466+
{
467+
$encapsed .= $part->toString();
468+
}
469+
}
470+
return $encapsed ?: null;
471+
}
472+
else if ($node->name instanceof Node\Identifier)
473+
{
474+
return $node->name->name;
475+
}
476+
else if ($node->name instanceof Node\Name)
477+
{
478+
return $node->name->getFirst();
479+
}
480+
else
481+
{
482+
return $node->name->toString();
483+
}
484+
return null;
432485
}
433486

434487
/**

tests/testFiles/var_test2.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class var_test2
88
public function __construct()
99
{
1010
$name = 'test';
11-
$result = $this->{'validate_' . $name}();
12-
}
11+
$result1 = $this->{'validate1_' . $name}();
12+
$result2 = $this->{"validate2_$name"}();
13+
$result3 = $this->{$name === 'test' ? 'callBack1' : 'callBack2'}();
14+
15+
}
1316
}

0 commit comments

Comments
 (0)