Skip to content

Commit

Permalink
Remove the blade complier
Browse files Browse the repository at this point in the history
  • Loading branch information
driftingly committed Dec 16, 2023
1 parent 3e998a4 commit 0c634e3
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 135 deletions.
54 changes: 1 addition & 53 deletions src/Formatters/UseAuthHelperOverFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Parser;
use PhpParser\PrettyPrinter\Standard;
use Tighten\TLint\BaseFormatter;
use Tighten\TLint\Illuminate\BladeCompiler;
use Tighten\TLint\Linters\UseAuthHelperOverFacade as Linter;

class UseAuthHelperOverFacade extends BaseFormatter
Expand Down Expand Up @@ -48,68 +47,17 @@ class UseAuthHelperOverFacade extends BaseFormatter
'shouldUse',
];

private $bladeCode;

public function __construct($code, $filename = null)
{
if (preg_match('/\.blade\.php$/i', $filename)) {
$bladeCompiler = new BladeCompiler(null, sys_get_temp_dir());
$this->bladeCode = $bladeCompiler->compileString($code);
}

parent::__construct($code, $filename);
}

public static function appliesToPath(string $path, array $configPaths): bool
{
return Linter::appliesToPath($path, $configPaths);
}

public function format(Parser $parser, Lexer $lexer): string
{
if ($this->bladeCode) {
return $this->formatBlade($parser, $lexer);
}

return $this->formatCode($this->code, $parser, $lexer);
}

private function formatBlade(Parser $parser, Lexer $lexer)
{
// Check if compiled blade code contains any Auth:: calls
if ($this->bladeCode === $this->formatCode($this->bladeCode, $parser, $lexer)) {
return $this->code;
}

// Find/replace in original blade code
foreach ($this->getCodeLines() as $index => $codeLine) {
$matches = [];

preg_match_all(
self::AUTH_SEARCH,
$codeLine,
$matches,
PREG_SET_ORDER
);

foreach ($matches as $match) {
if (in_array($match[1], self::AUTH_HELPER_METHODS)) {
$codeLine = str_replace($match[0], 'auth()->' . $match[1] . '(', $codeLine);

$this->code = $this->replaceCodeLine($index + 1, $codeLine);
}
}
}

return $this->code;
}

private function formatCode(string $code, Parser $parser, Lexer $lexer)
{
$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$oldStmts = $parser->parse($code);
$oldStmts = $parser->parse($this->code);
$newStmts = $traverser->traverse($oldStmts);

$traverser = new NodeTraverser;
Expand Down
21 changes: 0 additions & 21 deletions src/Illuminate/BladeCompiler.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Linters/UseAuthHelperOverFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@
use Closure;
use PhpParser\Node;
use Tighten\TLint\BaseLinter;
use Tighten\TLint\Illuminate\BladeCompiler;

class UseAuthHelperOverFacade extends BaseLinter
{
public const DESCRIPTION = 'Prefer the `auth()` helper function over the `Auth` Facade.';

public function __construct($code, $filename = null)
{
if (preg_match('/\.blade\.php$/i', $filename)) {
$bladeCompiler = new BladeCompiler(null, sys_get_temp_dir());
$code = $bladeCompiler->compileString($code);
}

parent::__construct($code, $filename);
}

protected function visitor(): Closure
{
return function (Node $node) {
Expand Down
30 changes: 0 additions & 30 deletions tests/Formatting/Formatters/UseAuthHelperOverFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,6 @@

class UseAuthHelperOverFacadeTest extends TestCase
{
/** @test */
public function catches_auth_facade_usage_in_views()
{
$file = <<<'file'
@extends('layouts.app')
@if (!\Illuminate\Support\Facades\Auth::check())
<label for="email">Email</label>
<input id="email" class="form-control" type="email" name="email" required>
@else
<input id="email" type="hidden" name="email" value="{{ auth()->user()->email }}" required>
@endif
file;

$correctlyFormatted = <<<'file'
@extends('layouts.app')
@if (!auth()->check())
<label for="email">Email</label>
<input id="email" class="form-control" type="email" name="email" required>
@else
<input id="email" type="hidden" name="email" value="{{ auth()->user()->email }}" required>
@endif
file;

$formatted = (new TFormat)->format(
new UseAuthHelperOverFacade($file, '.blade.php')
);

$this->assertEquals($correctlyFormatted, $formatted);
}

/** @test */
public function catches_auth_facade_usage_in_code()
{
Expand Down
20 changes: 0 additions & 20 deletions tests/Linting/Linters/UseAuthHelperOverFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@

class UseAuthHelperOverFacadeTest extends TestCase
{
/** @test */
public function catches_auth_facade_usage_in_views()
{
$file = <<<file
@extends('layouts.app')
@if (!\Illuminate\Support\Facades\Auth::check())
<label for="email">Email</label>
<input id="email" class="form-control" type="email" name="email" required>
@else
<input id="email" type="hidden" name="email" value="{{ auth()->user()->email }}" required>
@endif
file;

$lints = (new TLint)->lint(
new UseAuthHelperOverFacade($file, '.blade.php')
);

$this->assertNotNull($lints[0]);
}

/** @test */
public function catches_auth_facade_usage_in_code()
{
Expand Down

0 comments on commit 0c634e3

Please sign in to comment.