Skip to content

Commit

Permalink
Merge pull request #362 from tighten/drift/update-php-parser
Browse files Browse the repository at this point in the history
Upgrade PHP Parser to 5.0
  • Loading branch information
driftingly authored Apr 26, 2024
2 parents 1efca15 + 6be0770 commit 9999dc0
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": ">=8.1",
"illuminate/view": "*",
"nikic/php-parser": "^4.15",
"nikic/php-parser": "^5.0",
"symfony/console": "^6.1||^7.0",
"symfony/process": "^6.1||^7.0"
},
Expand Down
3 changes: 1 addition & 2 deletions src/BaseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Tighten\TLint;

use PhpParser\Lexer;
use PhpParser\Parser;

class BaseFormatter extends AbstractBase
{
public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
return $this->code;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Formatters/ArrayParametersOverViewWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Tighten\TLint\Formatters;

use Closure;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\NodeTraverser;
Expand All @@ -27,19 +26,21 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$traverser = new NodeTraverser;
$traverser->addVisitor($this->visitor());
$newStmts = $traverser->traverse($newStmts);

return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens()));
return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function visitor(): NodeVisitorAbstract
Expand Down
11 changes: 6 additions & 5 deletions src/Formatters/FullyQualifiedFacades.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Tighten\TLint\Formatters;

use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UseItem;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitorAbstract;
Expand All @@ -21,12 +20,14 @@ class FullyQualifiedFacades extends BaseFormatter

public const DESCRIPTION = 'Import facades using their full namespace.';

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$traverser = new NodeTraverser;
Expand All @@ -43,7 +44,7 @@ public function format(Parser $parser, Lexer $lexer): string
$traverser->addVisitor($this->transformFacadesVisitor());
$newStmts = $traverser->traverse($newStmts);

return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens()));
return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function currentFullyQualifiedFacadesVisitor(): NodeVisitorAbstract
Expand Down Expand Up @@ -111,7 +112,7 @@ public function enterNode(Node $node): Node|int|null
return null;
}

return new Use_([new UseUse(new Name($facades[$useStatement]))]);
return new Use_([new UseItem(new Name($facades[$useStatement]))]);
}
};
}
Expand Down
7 changes: 4 additions & 3 deletions src/Formatters/MailableMethodsInBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tighten\TLint\Formatters;

use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
Expand All @@ -22,12 +21,14 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$constructorVisitor = $this->constructorVisitor();
Expand All @@ -42,7 +43,7 @@ public function format(Parser $parser, Lexer $lexer): string
$traverser->addVisitor($buildVisitor);
$newStmts = $traverser->traverse($newStmts);

return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens()));
return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function constructorVisitor(): NodeVisitorAbstract
Expand Down
31 changes: 16 additions & 15 deletions src/Formatters/NoDatesPropertyOnModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace Tighten\TLint\Formatters;

use Closure;
use PhpParser\Lexer;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
Expand All @@ -27,22 +26,24 @@ class NoDatesPropertyOnModels extends BaseFormatter

protected bool $model = false;

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$originalStatements = $parser->parse($this->code);
$statements = $traverser->traverse($originalStatements);
$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$nodeFinder = new NodeFinder;
$dates = $nodeFinder->findFirst($statements, $this->nodeFinderForModelProperty('dates'));
$casts = $nodeFinder->findFirst($statements, $this->nodeFinderForModelProperty('casts'));
$dates = $nodeFinder->findFirst($newStmts, $this->nodeFinderForModelProperty('dates'));
$casts = $nodeFinder->findFirst($newStmts, $this->nodeFinderForModelProperty('casts'));

if ($dates) {
$newCasts = $this->addDatesToCasts($dates, $casts);

$statements = array_map(function (Node $node) use ($newCasts) {
$newStmts = array_map(function (Node $node) use ($newCasts) {
if ($this->extendsAny($node, ['Model', 'Pivot', 'Authenticatable'])) {
// Replace the dates with the new casts and remove any existing casts (necessary so
// that we know where to put the new casts if there weren't any already)
Expand All @@ -58,10 +59,10 @@ public function format(Parser $parser, Lexer $lexer): string
}

return $node;
}, $statements);
}, $newStmts);
}

return preg_replace('/\r?\n/', PHP_EOL, $this->printer()->printFormatPreserving($statements, $originalStatements, $lexer->getTokens()));
return preg_replace('/\r?\n/', PHP_EOL, $this->printer()->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function nodeFinderForModelProperty(string $attribute): Closure
Expand Down Expand Up @@ -110,8 +111,8 @@ private function addDatesToCasts(Property $dates, ?Property $casts = null): Prop

// We have to return a *new* Property node here (even if there was
// already a casts property) so the printer formats it correctly
return new Property(Class_::MODIFIER_PROTECTED, [
new PropertyProperty('casts', new Array_($newCasts, ['kind' => Array_::KIND_SHORT])),
return new Property(Modifiers::PROTECTED, [
new PropertyItem('casts', new Array_($newCasts, ['kind' => Array_::KIND_SHORT])),
]);
}

Expand All @@ -120,7 +121,7 @@ private function printer(): Standard
return new class extends Standard
{
// Force all arrays to be printed in multiline style
protected function pMaybeMultiline(array $nodes, bool $trailingComma = true)
protected function pMaybeMultiline(array $nodes, bool $trailingComma = true): string
{
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Formatters/NoDocBlocksForMigrationUpDown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tighten\TLint\Formatters;

use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\FindingVisitor;
Expand All @@ -16,7 +15,7 @@ class NoDocBlocksForMigrationUpDown extends BaseFormatter

public const DESCRIPTION = 'Removes doc blocks from the up and down method in migrations.';

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$oldStmts = $parser->parse($this->code);

Expand Down
7 changes: 4 additions & 3 deletions src/Formatters/NoLeadingSlashesOnRoutePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tighten\TLint\Formatters;

use Illuminate\Support\Str;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
Expand All @@ -25,20 +24,22 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$traverser = new NodeTraverser;
$traverser->addVisitor($this->visitor());

$newStmts = $traverser->traverse($newStmts);

return (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens());
return (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
}

private function visitor(): NodeVisitorAbstract
Expand Down
3 changes: 1 addition & 2 deletions src/Formatters/NoSpaceAfterBladeDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tighten\TLint\Formatters;

use Illuminate\Support\Str;
use PhpParser\Lexer;
use PhpParser\Parser;
use Tighten\TLint\BaseFormatter;
use Tighten\TLint\Linters\NoSpaceAfterBladeDirectives as Linter;
Expand All @@ -17,7 +16,7 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
foreach ($this->getCodeLines() as $index => $codeLine) {
$matches = [];
Expand Down
9 changes: 5 additions & 4 deletions src/Formatters/OneLineBetweenClassVisibilityChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tighten\TLint\Formatters;

use PhpParser\Comment;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
Expand All @@ -24,19 +23,21 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$traverser = new NodeTraverser;
$traverser->addVisitor($this->visitor());
$newStmts = $traverser->traverse($newStmts);

return preg_replace('/\ *\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens()));
return preg_replace('/\ *\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function visitor(): NodeVisitorAbstract
Expand Down Expand Up @@ -103,7 +104,7 @@ public function enterNode(Node $node)
}
}

$node->setAttribute('comments', [new Comment("\n"), ...$node->getComments()]);
$node->setAttribute('comments', [new Comment(''), ...$node->getComments()]);

return $node;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Formatters/RemoveLeadingSlashNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tighten\TLint\Formatters;

use Illuminate\Support\Str;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
Expand All @@ -21,7 +20,7 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);
Expand Down Expand Up @@ -63,7 +62,7 @@ public function getReplacements()

public function enterNode(Node $node): Node|int|null
{
if (! $node instanceof Node\Stmt\UseUse) {
if (! $node instanceof Node\UseItem) {
return null;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Formatters/RequestHelperFunctionWherePossible.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tighten\TLint\Formatters;

use Closure;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
Expand All @@ -24,12 +23,14 @@ public static function appliesToPath(string $path, array $configPaths): bool
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
public function format(Parser $parser): string
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($this->code);
$oldTokens = $parser->getTokens();

$newStmts = $traverser->traverse($oldStmts);

$visitor = $this->visitor();
Expand All @@ -38,7 +39,7 @@ public function format(Parser $parser, Lexer $lexer): string
$traverser->addVisitor($visitor);
$newStmts = $traverser->traverse($newStmts);

return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $lexer->getTokens()));
return preg_replace('/\r?\n/', PHP_EOL, (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens));
}

private function visitor(): NodeVisitorAbstract
Expand Down
Loading

0 comments on commit 9999dc0

Please sign in to comment.