Skip to content

Commit 85c4ba4

Browse files
committed
Fix
1 parent 002f7cc commit 85c4ba4

13 files changed

Lines changed: 90 additions & 39 deletions

File tree

generator/src/Commands/GenerateCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,19 @@ protected function execute(
8383
$modules[$function->getModuleName()] = true;
8484
}
8585

86-
$genDir = FileCreator::getSafeRootDir() . "/generated/$version";
86+
$genDir = Filesystem::outputDir() . "/$version";
8787
$fileCreator = new FileCreator();
8888
$fileCreator->generatePhpFile($res->methods, "$genDir/");
8989
$fileCreator->generateFunctionsList($res->methods, "$genDir/functionsList.php");
9090
$fileCreator->generateRectorFile($res->methods, "$genDir/rector-migrate.php");
9191
}
9292

9393
foreach (\array_keys($modules) as $moduleName) {
94-
$fileCreator->generateVersionSplitters($moduleName, FileCreator::getSafeRootDir() . "/generated/", \array_keys($versions));
94+
$fileCreator->generateVersionSplitters($moduleName, Filesystem::outputDir() . "/", \array_keys($versions));
9595
$fileCreator->createExceptionFile((string) $moduleName);
9696
}
97-
$fileCreator->generateVersionSplitters("functionsList", FileCreator::getSafeRootDir() . "/generated/", \array_keys($versions), true);
97+
98+
$fileCreator->generateVersionSplitters("functionsList", Filesystem::outputDir() . "/", \array_keys($versions), true);
9899

99100
$this->runCsFix($output);
100101

generator/src/Generator/ComposerJsonEditor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Safe\Generator;
66

7+
use Safe\Templating\Filesystem;
8+
79
/**
810
* This class will edit the main composer.json file to add the list of files generated from modules.
911
*/
@@ -15,7 +17,7 @@ class ComposerJsonEditor
1517
public static function editComposerFileForGeneration(array $modules): void
1618
{
1719

18-
$composerContent = file_get_contents(FileCreator::getSafeRootDir() . '/composer.json');
20+
$composerContent = file_get_contents(Filesystem::projectRootDir() . '/composer.json');
1921
if ($composerContent === false) {
2022
throw new \RuntimeException('Error while loading composer.json file for edition.');
2123
}
@@ -24,7 +26,7 @@ public static function editComposerFileForGeneration(array $modules): void
2426
$composerJson['autoload']['files'] = self::editFilesListForGeneration($composerJson['autoload']['files'], $modules);
2527

2628
$newContent = \json_encode($composerJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) . "\n";
27-
\file_put_contents(FileCreator::getSafeRootDir() . '/composer.json', $newContent);
29+
\file_put_contents(Filesystem::projectRootDir() . '/composer.json', $newContent);
2830
}
2931

3032
/**

generator/src/Generator/FileCreator.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Safe\Generator;
66

77
use Safe\Templating\Engine;
8+
use Safe\Templating\Filesystem;
89
use Safe\XmlDocParser\ErrorType;
910
use Safe\XmlDocParser\Scanner;
1011
use Safe\XmlDocParser\Method;
@@ -45,10 +46,10 @@ public function generatePhpFile(
4546
\mkdir($path);
4647
}
4748

48-
$this->dumpTemplate($path . $lcModule . '.php', 'Module.php.tpl', [
49+
Filesystem::dumpFile($path . $lcModule . '.php', $this->engine->generate('Module.php.tpl', [
4950
'{{exceptionName}}' => self::toExceptionName($module),
5051
'{{functions}}' => \implode(PHP_EOL, $phpFunctions),
51-
]);
52+
]));
5253
}
5354
}
5455

@@ -102,9 +103,9 @@ private function getFunctionsNameList(array $functions): array
102103
*/
103104
public function generateFunctionsList(array $functions, string $path): void
104105
{
105-
$this->dumpTemplate($path, 'FunctionList.php.tpl', [
106+
Filesystem::dumpFile($path, $this->engine->generate('FunctionList.php.tpl', [
106107
'{{functionNames}}' => \implode(PHP_EOL, \array_map(static fn(string $name): string => \sprintf('\'%s\',', $name), $this->getFunctionsNameList($functions))),
107-
]);
108+
]));
108109
}
109110

110111
/**
@@ -114,23 +115,29 @@ public function generateFunctionsList(array $functions, string $path): void
114115
*/
115116
public function generateRectorFile(array $functions, string $path): void
116117
{
117-
$this->dumpTemplate($path, 'RectorConfig.php.tpl', [
118+
Filesystem::dumpFile($path, $this->engine->generate('RectorConfig.php.tpl', [
118119
'{{functionNames}}' => \implode(PHP_EOL, \array_map(static fn(string $name): string => \sprintf('\'%1$s\' => \'Safe\\%1$s\',', $name), $this->getFunctionsNameList($functions))),
119-
]);
120+
]));
120121
}
121122

122123
public function createExceptionFile(string $moduleName): void
123124
{
124125
$exceptionName = self::toExceptionName($moduleName);
125126

126-
$this->dumpTemplate(FileCreator::getSafeRootDir() . '/generated/Exceptions/'.$exceptionName.'.php', 'Exception.php.tpl', [
127+
Filesystem::dumpFile(Filesystem::outputDir() . '/Exceptions/'.$exceptionName.'.php', $this->engine->generate('Exception.php.tpl', [
127128
'{{exceptionName}}' => $exceptionName,
128-
]);
129+
]));
129130
}
130131

131132
public static function getSafeRootDir(): string
132133
{
133-
return __DIR__ . '/../../..';
134+
$path = realpath(__DIR__ . '/../../..');
135+
136+
if (false === $path) {
137+
throw new \RuntimeException('Unable to locate root directory');
138+
}
139+
140+
return $path;
134141
}
135142

136143
/**
@@ -140,16 +147,4 @@ public static function toExceptionName(string $moduleName): string
140147
{
141148
return str_replace('-', '', \ucfirst($moduleName)).'Exception';
142149
}
143-
144-
/**
145-
* @param array<string, string> $context
146-
*/
147-
private function dumpTemplate(string $target, string $template, array $context = []): void
148-
{
149-
$result = file_put_contents($target, $this->engine->generate($template, $context));
150-
151-
if (false === $result) {
152-
throw new \RuntimeException(\sprintf('Could not write to "%s".', $target));
153-
}
154-
}
155150
}

generator/src/PhpStanFunctions/PhpStanFunctionMapReader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Safe\PhpStanFunctions;
66

77
use Safe\Generator\FileCreator;
8+
use Safe\Templating\Filesystem;
89

910
class PhpStanFunctionMapReader
1011
{
@@ -20,8 +21,8 @@ class PhpStanFunctionMapReader
2021

2122
public function __construct()
2223
{
23-
$this->functionMap = require 'phar://' . FileCreator::getSafeRootDir() . '/generator/vendor/phpstan/phpstan/phpstan.phar/resources/functionMap.php';
24-
$this->customFunctionMap = require FileCreator::getSafeRootDir() . '/generator/config/CustomPhpStanFunctionMap.php';
24+
$this->functionMap = require 'phar://' . Filesystem::generatorDir() . '/vendor/phpstan/phpstan/phpstan.phar/resources/functionMap.php';
25+
$this->customFunctionMap = require Filesystem::generatorDir() . '/config/CustomPhpStanFunctionMap.php';
2526
}
2627

2728
public function getFunction(string $functionName): ?PhpStanFunction

generator/src/Templating/Engine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ private function hasTemplate(string $name): bool
5656

5757
private static function basePath(): string
5858
{
59-
return __DIR__ . '/templates';
59+
return Filesystem::generatorDir().'/templates';
6060
}
6161
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Safe\Templating;
6+
7+
final class Filesystem
8+
{
9+
private function __construct()
10+
{
11+
}
12+
13+
public static function projectRootDir(): string
14+
{
15+
$path = realpath(__DIR__ . '/../../..');
16+
17+
if (false === $path) {
18+
throw new \RuntimeException('Unable to locate root directory');
19+
}
20+
21+
return $path;
22+
}
23+
24+
public static function generatorDir(): string
25+
{
26+
return \sprintf(self::projectRootDir().'/generator');
27+
}
28+
29+
public static function outputDir(): string
30+
{
31+
return \sprintf(self::projectRootDir().'/generated');
32+
}
33+
34+
public static function dumpFile(string $targetPath, string $content): void
35+
{
36+
$result = file_put_contents($targetPath, $content);
37+
38+
if (false === $result) {
39+
throw new \RuntimeException(\sprintf('Could not write to "%s".', $targetPath));
40+
}
41+
}
42+
}

generator/src/XmlDocParser/DocPage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Safe\Generator\FileCreator;
88

9+
use Safe\Templating\Filesystem;
910
use function explode;
1011
use function strpos;
1112

@@ -61,7 +62,7 @@ public function getErrorType(): ErrorType
6162
// This minimizes 'false positives', where text such as "returns false when ..." could be matched outside
6263
// the function's dedicated Return Values section.
6364
$returnDocs = $this->extractSection('returnvalues', $file);
64-
$detectErrorType = require FileCreator::getSafeRootDir() . '/generator/config/detectErrorType.php';
65+
$detectErrorType = require Filesystem::generatorDir() . '/config/detectErrorType.php';
6566
return $detectErrorType($returnDocs);
6667
}
6768

generator/src/XmlDocParser/Scanner.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Safe\XmlDocParser;
66

7+
use Safe\Templating\Filesystem;
8+
use Symfony\Component\Console\Style\SymfonyStyle;
79
use function array_merge;
810
use function iterator_to_array;
911
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
@@ -58,7 +60,7 @@ public function getMethodsPaths(): array
5860
private function getIgnoredFunctions(): array
5961
{
6062
if ($this->ignoredFunctions === null) {
61-
$ignoredFunctions = require FileCreator::getSafeRootDir() . '/generator/config/ignoredFunctions.php';
63+
$ignoredFunctions = require Filesystem::generatorDir() . '/config/ignoredFunctions.php';
6264
$specialCaseFunctions = $this->getSpecialCases();
6365

6466
$this->ignoredFunctions = array_merge($ignoredFunctions, $specialCaseFunctions);
@@ -73,7 +75,7 @@ private function getIgnoredFunctions(): array
7375
private function getIgnoredModules(): array
7476
{
7577
if ($this->ignoredModules === null) {
76-
$this->ignoredModules = require FileCreator::getSafeRootDir() . '/generator/config/ignoredModules.php';
78+
$this->ignoredModules = require Filesystem::generatorDir() . '/config/ignoredModules.php';
7779
assert(!is_null($this->ignoredModules));
7880
}
7981
return $this->ignoredModules;
@@ -87,7 +89,7 @@ private function getIgnoredModules(): array
8789
*/
8890
public static function getSpecialCases(): array
8991
{
90-
$data = file_get_contents(FileCreator::getSafeRootDir() . '/lib/special_cases.php');
92+
$data = file_get_contents(Filesystem::projectRootDir() . '/lib/special_cases.php');
9193
if ($data === false) {
9294
throw new \RuntimeException('Unable to read special cases');
9395
}
@@ -104,14 +106,14 @@ public static function getSpecialCases(): array
104106
*/
105107
public static function getHiddenFunctions(): array
106108
{
107-
return require FileCreator::getSafeRootDir() . '/generator/config/hiddenFunctions.php';
109+
return require Filesystem::generatorDir() . '/config/hiddenFunctions.php';
108110
}
109111

110112
/**
111113
* @param SplFileInfo[] $paths
112114
* @param string[] $pastFunctionNames
113115
*/
114-
public function getMethods(array $paths, array $pastFunctionNames, OutputInterface $output): ScannerResponse
116+
public function getMethods(array $paths, array $pastFunctionNames, SymfonyStyle $io): ScannerResponse
115117
{
116118
/** @var Method[] $functions */
117119
$functions = [];
@@ -124,9 +126,7 @@ public function getMethods(array $paths, array $pastFunctionNames, OutputInterfa
124126
$ignoredModules = $this->getIgnoredModules();
125127
$ignoredModules = \array_combine($ignoredModules, $ignoredModules);
126128

127-
ProgressBar::setFormatDefinition('custom', ' %current%/%max% [%bar%] %message%');
128-
$progressBar = new ProgressBar($output, count($paths));
129-
$progressBar->setFormat("custom");
129+
$progressBar = $io->createProgressBar(\count($paths));
130130
foreach ($paths as $path) {
131131
$module = \basename(\dirname($path->getPath()));
132132
$progressBar->setMessage($path->getFilename());
@@ -167,8 +167,8 @@ public function getMethods(array $paths, array $pastFunctionNames, OutputInterfa
167167
}
168168
}
169169
}
170+
170171
$progressBar->finish();
171-
$output->writeln("");
172172

173173
return new ScannerResponse($functions, $overloadedFunctions);
174174
}

generator/src/XmlDocParser/ScannerResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function __construct(
1515
public readonly array $overloadedFunctions
1616
) {
1717
}
18+
19+
public function hasOverloadedFunctions(): bool
20+
{
21+
return \count($this->overloadedFunctions) > 0;
22+
}
1823
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace Safe\Exceptions;
36

47
class {{exceptionName}} extends \ErrorException implements SafeExceptionInterface
58
{
69
public static function createFromPhpError(): self
710
{
811
$error = error_get_last();
12+
913
return new self($error['message'] ?? 'An error occurred', 0, $error['type'] ?? 1);
1014
}
1115
}

0 commit comments

Comments
 (0)