-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix external packages bleeding edge includes (#3389)
- Loading branch information
1 parent
d8aa22b
commit 9c6f41e
Showing
7 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...olver/DependencyInjection/BleedingEdgeIncludePurifier/BleedingEdgeIncludePurifierTest.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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NodeTypeResolver\DependencyInjection\BleedingEdgeIncludePurifier; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Rector\NodeTypeResolver\DependencyInjection\BleedingEdgeIncludePurifier; | ||
use Rector\Testing\PHPUnit\AbstractTestCase; | ||
|
||
final class BleedingEdgeIncludePurifierTest extends AbstractTestCase | ||
{ | ||
private BleedingEdgeIncludePurifier $bleedingEdgeIncludePurifier; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->boot(); | ||
|
||
$this->bleedingEdgeIncludePurifier = $this->getService(BleedingEdgeIncludePurifier::class); | ||
} | ||
|
||
public function testNothing(): void | ||
{ | ||
$purifiedConfigFilePath = $this->bleedingEdgeIncludePurifier->purifyConfigFile( | ||
__DIR__ . '/Fixture/no_bleeding_edge.neon' | ||
); | ||
$this->assertNull($purifiedConfigFilePath); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$purifiedConfigFilePath = $this->bleedingEdgeIncludePurifier->purifyConfigFile( | ||
__DIR__ . '/Fixture/some_file_including.neon' | ||
); | ||
|
||
$this->assertNotNull($purifiedConfigFilePath); | ||
|
||
$this->assertFileEquals(__DIR__ . '/Expected/some_file_including.neon', $purifiedConfigFilePath); | ||
|
||
// cleanup after yourself :) | ||
FileSystem::delete($purifiedConfigFilePath); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...esolver/DependencyInjection/BleedingEdgeIncludePurifier/Expected/some_file_including.neon
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,4 @@ | ||
includes: | ||
|
||
parameters: | ||
one: two |
2 changes: 2 additions & 0 deletions
2
...ypeResolver/DependencyInjection/BleedingEdgeIncludePurifier/Fixture/no_bleeding_edge.neon
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 @@ | ||
parameters: | ||
one: two |
5 changes: 5 additions & 0 deletions
5
...Resolver/DependencyInjection/BleedingEdgeIncludePurifier/Fixture/some_file_including.neon
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,5 @@ | ||
includes: | ||
- %rootDir%/conf/bleedingEdge.neon | ||
|
||
parameters: | ||
one: two |
27 changes: 27 additions & 0 deletions
27
packages-tests/NodeTypeResolver/DependencyInjection/PHPStanServiceFactoryTest.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NodeTypeResolver\DependencyInjection; | ||
|
||
use PHPStan\Parser\Parser; | ||
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory; | ||
use Rector\Testing\PHPUnit\AbstractTestCase; | ||
|
||
final class PHPStanServiceFactoryTest extends AbstractTestCase | ||
{ | ||
private PHPStanServicesFactory $phpStanServicesFactory; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->boot(); | ||
|
||
$this->phpStanServicesFactory = $this->getService(PHPStanServicesFactory::class); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$phpstanParser = $this->phpStanServicesFactory->createPHPStanParser(); | ||
$this->assertInstanceOf(Parser::class, $phpstanParser); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/NodeTypeResolver/DependencyInjection/BleedingEdgeIncludePurifier.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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\DependencyInjection; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Strings; | ||
|
||
/** | ||
* Prevents failing include of bleeding edge in of phpstan extensions. | ||
* @see https://github.com/rectorphp/rector/issues/2431 | ||
* | ||
* Do not delete this. It's not tested here, but is needed to avoid this rare case happen. It's not possible to solve otherwise. | ||
* @see https://github.com/rectorphp/rector-src/commit/70fb9af2fdfa55db63c68c6dac6723fe55cec1a0 | ||
* @eee https://github.com/rectorphp/rector/pull/2550 | ||
* | ||
* @see \Rector\Tests\NodeTypeResolver\DependencyInjection\BleedingEdgeIncludePurifier\BleedingEdgeIncludePurifierTest | ||
*/ | ||
final class BleedingEdgeIncludePurifier | ||
{ | ||
/** | ||
* @see https://regex101.com/r/CWADBe/2 | ||
* @var string | ||
*/ | ||
private const BLEEDING_EDGE_REGEX = '#\n\s+-(.*?)bleedingEdge\.neon[\'|"]?#'; | ||
|
||
public function purifyConfigFile(string $filePath): ?string | ||
{ | ||
// must be neon file | ||
if (! str_ends_with($filePath, '.neon')) { | ||
return null; | ||
} | ||
|
||
$fileContents = FileSystem::read($filePath); | ||
|
||
// bleeding edge clean out, see https://github.com/rectorphp/rector/issues/2431 | ||
$matches = Strings::match($fileContents, self::BLEEDING_EDGE_REGEX); | ||
if ($matches === null) { | ||
return null; | ||
} | ||
|
||
$temporaryFilePath = $this->createTemporaryFilePath($filePath); | ||
|
||
$clearedFileContents = Strings::replace($fileContents, self::BLEEDING_EDGE_REGEX); | ||
FileSystem::write($temporaryFilePath, $clearedFileContents); | ||
|
||
return $temporaryFilePath; | ||
} | ||
|
||
private function createTemporaryFilePath(string $filePath): string | ||
{ | ||
$fileDirectory = dirname($filePath); | ||
$baseFileName = pathinfo($filePath, PATHINFO_BASENAME); | ||
|
||
return $fileDirectory . '/temp_' . $baseFileName; | ||
} | ||
} |
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