Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ability to fix variable names (v1) #285

Merged
merged 1 commit into from Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 5 additions & 73 deletions src/Solutions/SuggestCorrectVariableNameSolution.php
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
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