-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add YAML usage class constant support (#53)
- Loading branch information
1 parent
9c62e4f
commit 72162d1
Showing
5 changed files
with
101 additions
and
2 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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\SwissKnife\YAML; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Strings; | ||
use Rector\SwissKnife\Contract\ClassConstantFetchInterface; | ||
use Rector\SwissKnife\Finder\FilesFinder; | ||
use Rector\SwissKnife\ValueObject\ClassConstantFetch\ExternalClassAccessConstantFetch; | ||
|
||
/** | ||
* @see \Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\YamlConfigConstantExtractorTest | ||
*/ | ||
final class YamlConfigConstantExtractor | ||
{ | ||
/** | ||
* @param string[] $directories | ||
* @return ClassConstantFetchInterface[] | ||
*/ | ||
public function extractFromDirs(array $directories): array | ||
{ | ||
$yamlFileInfos = FilesFinder::findYamlFiles($directories); | ||
|
||
$classConstantFetches = []; | ||
foreach ($yamlFileInfos as $yamlFileInfo) { | ||
$currentClassConstantFetches = $this->findClassConstantFetchesInFile($yamlFileInfo->getRealPath()); | ||
|
||
$classConstantFetches = array_merge($classConstantFetches, $currentClassConstantFetches); | ||
} | ||
|
||
return $classConstantFetches; | ||
} | ||
|
||
/** | ||
* @return ClassConstantFetchInterface[] | ||
*/ | ||
private function findClassConstantFetchesInFile(string $filePath): array | ||
{ | ||
$fileContents = FileSystem::read($filePath); | ||
|
||
// find constant fetches in YAML file | ||
$constantMatches = Strings::matchAll($fileContents, '#\b(?<class>[\w\\\]+)::(?<constant>\w+)\b#'); | ||
|
||
$externalClassAccessConstantFetches = []; | ||
foreach ($constantMatches as $constantMatch) { | ||
$externalClassAccessConstantFetches[] = new ExternalClassAccessConstantFetch( | ||
$constantMatch['class'], | ||
$constantMatch['constant'] | ||
); | ||
} | ||
|
||
return $externalClassAccessConstantFetches; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/YAML/YamlConfigConstantExtractor/Fixture/SomeClassWithConstant.php
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,8 @@ | ||
<?php | ||
|
||
namespace Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\Fixture; | ||
|
||
final class SomeClassWithConstant | ||
{ | ||
public const USE_ME_IN_YAML = 'value'; | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/YAML/YamlConfigConstantExtractor/Fixture/some_file.yaml
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,2 @@ | ||
some_config: | ||
value: <(\Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\Fixture\SomeClassWithConstant::USE_ME_IN_YAML)> |
25 changes: 25 additions & 0 deletions
25
tests/YAML/YamlConfigConstantExtractor/YamlConfigConstantExtractorTest.php
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor; | ||
|
||
use Rector\SwissKnife\Tests\AbstractTestCase; | ||
use Rector\SwissKnife\YAML\YamlConfigConstantExtractor; | ||
|
||
final class YamlConfigConstantExtractorTest extends AbstractTestCase | ||
{ | ||
private YamlConfigConstantExtractor $yamlConfigConstantExtractor; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->yamlConfigConstantExtractor = $this->make(YamlConfigConstantExtractor::class); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$classConstantFetches = $this->yamlConfigConstantExtractor->extractFromDirs([__DIR__ . '/Fixture']); | ||
$this->assertCount(1, $classConstantFetches); | ||
} | ||
} |