Skip to content

Commit

Permalink
wip (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze authored Jul 13, 2020
1 parent af05ac5 commit c606a03
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 118 deletions.
78 changes: 5 additions & 73 deletions src/Solutions/SuggestCorrectVariableNameSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Facade\Ignition\Solutions;

use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Facades\Blade;
use Facade\IgnitionContracts\Solution;

class SuggestCorrectVariableNameSolution implements RunnableSolution
class SuggestCorrectVariableNameSolution implements Solution
{
/** @var string */
private $variableName;
Expand All @@ -30,82 +29,15 @@ public function getDocumentationLinks(): array
return [];
}

public function getSolutionActionDescription(): string
public function getSolutionDescription(): string
{
$path = str_replace(base_path().'/', '', $this->viewFile);

return "Did you mean `$$this->suggested`?";
}

public function getRunButtonText(): string
{
return 'Fix typo';
}

public function getSolutionDescription(): string
{
return '';
}

public function getRunParameters(): array
{
return [
'variableName' => $this->variableName,
'viewFile' => $this->viewFile,
'suggested' => $this->suggested,
];
}

public function isRunnable(array $parameters = []): bool
{
return $this->fixTypo($this->getRunParameters()) !== false;
}

public function run(array $parameters = []): void
{
$output = $this->fixTypo($parameters);
if ($output === false) {
return;
}

file_put_contents($parameters['viewFile'], $output);
}

protected function fixTypo(array $parameters = [])
public function isRunnable(): bool
{
if (! $this->isAlphaNumericWithUnderscore($parameters['suggested'])) {
return false;
}

$originalContents = file_get_contents($parameters['viewFile']);
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['suggested'], $originalContents);

$originalTokens = token_get_all(Blade::compileString($originalContents));
$newTokens = token_get_all(Blade::compileString($newContents));

$expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName'], $parameters['suggested']);

if ($expectedTokens !== $newTokens) {
return false;
}

return $newContents;
}

protected function isAlphaNumericWithUnderscore(string $input): bool
{
return preg_match('/^[a-zA-Z]+[a-zA-Z0-9_]+$/', $input);
}

protected function generateExpectedTokens(array $originalTokens, string $variableName, string $suggested): array
{
$expectedTokens = $originalTokens;
foreach ($expectedTokens as $key => $token) {
if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) {
$expectedTokens[$key][1] = "$$suggested";
}
}

return $expectedTokens;
return false;
}
}
45 changes: 0 additions & 45 deletions tests/Solutions/UndefinedVariableSolutionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,6 @@ public function it_can_solve_the_exception()
$this->assertTrue($canSolve);
}

/** @test */
public function it_can_recommend_fixing_a_variable_name_typo()
{
$viewData = [
'footerDescription' => 'foo',
];

try {
view('undefined-variable-1', $viewData)->render();
} catch (ViewException $exception) {
$viewException = $exception;
}

$canSolve = app(UndefinedVariableSolutionProvider::class)->canSolve($viewException);
$this->assertTrue($canSolve);

/** @var \Facade\IgnitionContracts\Solution $solution */
$solutions = app(UndefinedVariableSolutionProvider::class)->getSolutions($viewException);
$this->assertTrue(Str::contains($solutions[0]->getSolutionActionDescription(), 'Did you mean `$footerDescription`?'));
$this->assertTrue(Str::contains($solutions[1]->getSolutionActionDescription(), 'Replace `{{ $footerDescriptin }}` with `{{ $footerDescriptin ?? \'\' }}`'));
}

/** @test */
public function it_can_fix_a_variable_name_typo()
{
$viewData = [
'footerDescription' => 'foo',
];

try {
view('undefined-variable-1', $viewData)->render();
} catch (ViewException $exception) {
$viewException = $exception;
}

$canSolve = app(UndefinedVariableSolutionProvider::class)->canSolve($viewException);
$this->assertTrue($canSolve);

/** @var \Facade\IgnitionContracts\Solution $solution */
$solutions = app(UndefinedVariableSolutionProvider::class)->getSolutions($viewException);
$parameters = $solutions[0]->getRunParameters();
$parameters['viewFile'] = tempnam(sys_get_temp_dir(), 'undefined-variable-blade');
$solutions[0]->run($parameters);
}

protected function getUndefinedVariableException(): ViewException
{
return new ViewException('Undefined variable: notSet (View: ./views/welcome.blade.php)');
Expand Down

0 comments on commit c606a03

Please sign in to comment.