Skip to content

Commit 81261e1

Browse files
committed
make use of laravel di
1 parent 6bfdd1f commit 81261e1

18 files changed

+199
-173
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
composer.lock
2-
/vendor
2+
/vendor
3+
4+
.phpunit.cache

bin/console.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
<?php
22

3-
// decoupled in own "*.php" file, so ECS, Rector and PHPStan works out of the box here
4-
53
declare(strict_types=1);
64

5+
use Rector\PhpParserNodesDocs\DependencyInjection\ContainerFactory;
6+
use Symfony\Component\Console\Application;
7+
use Symfony\Component\Console\Input\ArgvInput;
8+
use Symfony\Component\Console\Output\ConsoleOutput;
9+
10+
require_once __DIR__ . '/../vendor/autoload.php';
11+
12+
$containerFactory = new ContainerFactory();
13+
$container = $containerFactory->create();
14+
15+
/** @var Application $application */
16+
$application = $container->make(Application::class);
17+
18+
$input = new ArgvInput();
19+
$output = new ConsoleOutput();
20+
21+
$exitCode = $application->run($input, $output);
22+
exit($exitCode);

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"php": "^8.1",
1111
"symfony/console": "^6.3",
1212
"nikic/php-parser": "^4.16",
13-
"illuminate/container": "^10.15"
13+
"illuminate/container": "^10.15",
14+
"webmozart/assert": "^1.11"
1415
},
1516
"require-dev": {
1617
"symplify/easy-coding-standard": "^11.5",
1718
"phpstan/phpstan": "^1.10",
1819
"phpunit/phpunit": "^10.2",
19-
"rector/rector": "*"
20+
"rector/rector": "^0.17.6"
2021
},
2122
"autoload": {
2223
"psr-4": {

config/config.php

-22
This file was deleted.

ecs.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
return static function (ECSConfig $ecsConfig): void {
99
$ecsConfig->paths([
10-
__DIR__ . '/config',
1110
__DIR__ . '/src',
1211
__DIR__ . '/tests',
1312
]);

phpstan.neon

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ parameters:
22
level: max
33

44
paths:
5-
- config
65
- src
76
- tests
87

98
ignoreErrors:
109
-
11-
message: '#Cannot cast array<string\>\|bool\|string\|null to string#'
10+
message: '#Cannot cast (.*?) to string#'
1211
path: src/Command/DumpNodesCommand.php

phpunit.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
cacheDirectory=".phpunit.cache"
8+
>
9+
<testsuites>
10+
<testsuite name="unit">
11+
<directory>tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

rector.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->importNames();
11+
12+
$rectorConfig->paths([
13+
__DIR__ . '/src',
14+
__DIR__ . '/tests',
15+
]);
16+
17+
$rectorConfig->sets([
18+
LevelSetList::UP_TO_PHP_81
19+
]);
20+
};

src/Command/DumpNodesCommand.php

+9-41
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Input\InputOption;
1212
use Symfony\Component\Console\Output\OutputInterface;
13-
use Symfony\Component\Console\Style\SymfonyStyle;
14-
use Symplify\PackageBuilder\Console\ShellCode;
15-
use Symplify\SmartFileSystem\SmartFileInfo;
16-
use Symplify\SmartFileSystem\SmartFileSystem;
1713

1814
final class DumpNodesCommand extends Command
1915
{
@@ -22,42 +18,16 @@ final class DumpNodesCommand extends Command
2218
*/
2319
private const OUTPUT_FILE = 'output-file';
2420

25-
/**
26-
* @var MarkdownNodeInfosPrinter
27-
*/
28-
private $markdownNodeInfosPrinter;
29-
30-
/**
31-
* @var NodeInfosFactory
32-
*/
33-
private $nodeInfosFactory;
34-
35-
/**
36-
* @var SmartFileSystem
37-
*/
38-
private $smartFileSystem;
39-
40-
/**
41-
* @var SymfonyStyle
42-
*/
43-
private $symfonyStyle;
44-
4521
public function __construct(
46-
MarkdownNodeInfosPrinter $markdownNodeInfosPrinter,
47-
NodeInfosFactory $nodeInfosFactory,
48-
SmartFileSystem $smartFileSystem,
49-
SymfonyStyle $symfonyStyle
22+
private readonly MarkdownNodeInfosPrinter $markdownNodeInfosPrinter,
23+
private readonly NodeInfosFactory $nodeInfosFactory,
5024
) {
51-
$this->markdownNodeInfosPrinter = $markdownNodeInfosPrinter;
52-
$this->nodeInfosFactory = $nodeInfosFactory;
53-
$this->smartFileSystem = $smartFileSystem;
54-
$this->symfonyStyle = $symfonyStyle;
55-
5625
parent::__construct();
5726
}
5827

5928
protected function configure(): void
6029
{
30+
$this->setName('dump-nodes');
6131
$this->setDescription('Dump nodes overview');
6232

6333
$this->addOption(
@@ -74,18 +44,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7444
$outputFile = (string) $input->getOption(self::OUTPUT_FILE);
7545

7646
$nodeInfos = $this->nodeInfosFactory->create();
77-
7847
$printedContent = $this->markdownNodeInfosPrinter->print($nodeInfos);
79-
$this->smartFileSystem->dumpFile($outputFile, $printedContent);
8048

81-
$outputFileFileInfo = new SmartFileInfo($outputFile);
82-
$message = sprintf(
49+
file_put_contents($outputFile, $printedContent);
50+
51+
$output->writeln(sprintf(
8352
'Documentation for "%d" PhpParser Nodes was generated to "%s"',
8453
count($nodeInfos),
85-
$outputFileFileInfo->getRelativeFilePathFromCwd()
86-
);
87-
$this->symfonyStyle->success($message);
54+
$outputFile
55+
));
8856

89-
return ShellCode::SUCCESS;
57+
return self::SUCCESS;
9058
}
9159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParserNodesDocs\DependencyInjection;
6+
7+
use Illuminate\Container\Container;
8+
use Rector\PhpParserNodesDocs\Command\DumpNodesCommand;
9+
use Symfony\Component\Console\Application;
10+
11+
final class ContainerFactory
12+
{
13+
public function create(): Container
14+
{
15+
$container = new Container();
16+
17+
$container->singleton(Application::class, function (Container $container) {
18+
$application = new Application();
19+
20+
/** @var DumpNodesCommand $dumpNodesCommand */
21+
$dumpNodesCommand = $container->make(DumpNodesCommand::class);
22+
$application->add($dumpNodesCommand);
23+
24+
return $application;
25+
});
26+
27+
return $container;
28+
}
29+
}

src/Finder/PhpFilesFinder.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParserNodesDocs\Finder;
6+
7+
use Webmozart\Assert\Assert;
8+
9+
final class PhpFilesFinder
10+
{
11+
/**
12+
* @param string[] $paths
13+
* @return string[]
14+
*/
15+
public function findPhpFiles(array $paths): array
16+
{
17+
Assert::allFileExists($paths);
18+
19+
// fallback to config paths
20+
$filePaths = [];
21+
22+
foreach ($paths as $path) {
23+
if (is_file($path)) {
24+
$filePaths[] = $path;
25+
} else {
26+
$currentFilePaths = $this->findFilesUsingGlob($path);
27+
$filePaths = array_merge($filePaths, $currentFilePaths);
28+
}
29+
}
30+
31+
sort($filePaths);
32+
33+
Assert::allString($filePaths);
34+
Assert::allFileExists($filePaths);
35+
36+
return $filePaths;
37+
}
38+
39+
/**
40+
* @return string[]
41+
*/
42+
private function findFilesUsingGlob(string $directory): array
43+
{
44+
// Search for php files in the current directory
45+
/** @var string[] $phpFiles */
46+
$phpFiles = glob($directory . '/*.php');
47+
48+
// recursively search in subdirectories
49+
50+
/** @var string[] $subdirectories */
51+
$subdirectories = glob($directory . '/*', GLOB_ONLYDIR);
52+
53+
foreach ($subdirectories as $subdirectory) {
54+
// Merge the results from subdirectories
55+
$phpFiles = array_merge($phpFiles, $this->findFilesUsingGlob($subdirectory));
56+
}
57+
58+
return $phpFiles;
59+
}
60+
}

src/HttpKernel/PhpParserNodesDocsKernel.php

-16
This file was deleted.

0 commit comments

Comments
 (0)