-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace CodingSocks\LostInTranslation; | ||
|
||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitor\FindingVisitor; | ||
use PhpParser\Parser; | ||
use PhpParser\ParserFactory; | ||
|
||
class BladeNowdocFinder | ||
{ | ||
/** @var \PhpParser\Parser */ | ||
private Parser $parser; | ||
|
||
/** | ||
* @param \PhpParser\Parser|null $parser | ||
*/ | ||
public function __construct( | ||
Parser $parser = null, | ||
) { | ||
$this->parser = $parser ?? (new ParserFactory())->createForHostVersion(); | ||
} | ||
|
||
/** | ||
* Find occurrences of translations in a string. | ||
* | ||
* @param string $str | ||
* @return \PhpParser\Node[] | ||
*/ | ||
public function find(string $str): array | ||
{ | ||
$nodes = $this->parser->parse($str); | ||
|
||
if (empty($nodes)) { | ||
return []; | ||
} | ||
|
||
$visitor = new FindingVisitor(function ($node) { | ||
return $node instanceof String_ | ||
&& strtolower($node->getAttribute('docLabel', '')) === 'blade'; | ||
}); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); // For compatibility with ^4.10 | ||
|
||
$traverser->traverse($nodes); | ||
|
||
return $visitor->getFoundNodes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace CodingSocks\LostInTranslation\Tests; | ||
|
||
use CodingSocks\LostInTranslation\BladeNowdocFinder; | ||
use CodingSocks\LostInTranslation\TranslationFinder; | ||
|
||
class BladeNowdocFinderTest extends TestCase | ||
{ | ||
public function testBladeDirective() | ||
{ | ||
$str = <<<'EOD' | ||
<?php | ||
class ExampleObject | ||
{ | ||
/** | ||
* Create a new component instance. | ||
*/ | ||
public function skip() | ||
{ | ||
return <<<'eod' | ||
<div> | ||
<!-- This has to be undetected. --> | ||
</div> | ||
eod; | ||
} | ||
/** | ||
* Get the view / contents that represent the component. | ||
*/ | ||
public function keep(): string | ||
{ | ||
return <<<'blade' | ||
<div> | ||
<!-- This has to be detected. --> | ||
</div> | ||
blade; | ||
} | ||
} | ||
EOD; | ||
|
||
$finder = new BladeNowdocFinder(); | ||
|
||
$nodes = $finder->find($str); | ||
|
||
$this->assertCount(1, $nodes); | ||
} | ||
} |