Skip to content

Commit 86ab3b7

Browse files
committed
Organise generator into namespaces
Trying to make this project as easy to contribute to as possible - smaller modules make it easier to focus on parts relevant to an issue
1 parent e0392eb commit 86ab3b7

23 files changed

+73
-60
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,15 @@ Safe-PHP code is generated automatically from the PHP doc.
1717
### Generator
1818

1919
* `safe.php` is the CLI entry point, with a few utility commands, but the
20-
most important one is `generate`, which generates the Safe-PHP code.
21-
* `GenerateCommand` is the class that actually does the generation:
22-
* Call `Scanner` to get a list of all the PHP XML documentation files
23-
(returned in a `ScannerResponse`).
24-
* Use `DocPage` to parse each XML file and extract the relevant
25-
information.
26-
* The "relevant information" is a list of `Method`s, which have
27-
`Parameter`s, which have `Type`s.
28-
* (As well as taking some information from the PHP XML docs, we also
29-
take some type-hint information from PHPStan's type-hint database,
30-
and merge these sources together to get a more complete picture.)
31-
* Given a bunch of `Method` meta-data objects, `FileCreator` will create
32-
files in `generated/` and use `WritePhpFunction` to write safe wrappers
33-
for each function into those files.
20+
most important one is `\Safe\Commands\GenerateCommand`, which does the
21+
generation:
22+
* Use `\Safe\XmlDocParser` to parse the PHP XML documentation
23+
and pull out information about `Method`s, `Parameter`s, and
24+
`Type`s - try to figure out which methods are unsafe.
25+
* Use `\Safe\PhpStanFunctions` to get a bit of extra data from
26+
PHPStan's typehint database, and merge that with the XML info.
27+
* Given this list of unsafe functions, use `\Safe\Generator` to
28+
write a bunch of safe wrappers.
3429

3530
### End-Users
3631

generator/safe.php

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

55
require __DIR__.'/vendor/autoload.php';
66

7-
use Safe\GenerateCommand;
8-
use Safe\ScanObjectsCommand;
9-
use Safe\FunctionInfoCommand;
7+
use Safe\Commands\FunctionInfoCommand;
8+
use Safe\Commands\GenerateCommand;
9+
use Safe\Commands\ScanObjectsCommand;
1010
use Symfony\Component\Console\Application;
1111

1212
$application = new Application();

generator/src/FunctionInfoCommand.php renamed to generator/src/Commands/FunctionInfoCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Commands;
66

7+
use Safe\XmlDocParser\Scanner;
8+
use Safe\XmlDocParser\DocPage;
9+
use Safe\Generator\WritePhpFunction;
710
use Symfony\Component\Console\Command\Command;
811
use Symfony\Component\Console\Input\InputInterface;
912
use Symfony\Component\Console\Input\InputArgument;

generator/src/GenerateCommand.php renamed to generator/src/Commands/GenerateCommand.php

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

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Commands;
6+
7+
use Safe\XmlDocParser\Scanner;
8+
use Safe\XmlDocParser\DocPage;
9+
use Safe\Generator\FileCreator;
10+
use Safe\Generator\ComposerJsonEditor;
611

712
use Symfony\Component\Console\Command\Command;
813
use Symfony\Component\Console\Input\InputInterface;

generator/src/ScanObjectsCommand.php renamed to generator/src/Commands/ScanObjectsCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Commands;
66

7+
use Safe\XmlDocParser\Scanner;
8+
use Safe\XmlDocParser\DocPage;
79
use Symfony\Component\Console\Command\Command;
810
use Symfony\Component\Console\Input\InputInterface;
911
use Symfony\Component\Console\Output\OutputInterface;

generator/src/ComposerJsonEditor.php renamed to generator/src/Generator/ComposerJsonEditor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Generator;
66

77
/**
88
* This class will edit the main composer.json file to add the list of files generated from modules.

generator/src/FileCreator.php renamed to generator/src/Generator/FileCreator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Generator;
6+
7+
use Safe\XmlDocParser\Method;
68

79
use function array_merge;
810
use function file_exists;
@@ -151,7 +153,7 @@ public static function createFromPhpError(): self
151153

152154
public static function getSafeRootDir(): string
153155
{
154-
return __DIR__ . '/../..';
156+
return __DIR__ . '/../../..';
155157
}
156158

157159
/**

generator/src/WritePhpFunction.php renamed to generator/src/Generator/WritePhpFunction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace Safe;
5+
namespace Safe\Generator;
6+
7+
use Safe\XmlDocParser\Method;
8+
use Safe\XmlDocParser\Parameter;
69

710
class WritePhpFunction
811
{

generator/src/PhpStanFunctions/PhpStanFunctionMapReader.php

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

55
namespace Safe\PhpStanFunctions;
66

7+
use Safe\Generator\FileCreator;
8+
79
class PhpStanFunctionMapReader
810
{
911
/**
@@ -18,8 +20,8 @@ class PhpStanFunctionMapReader
1820

1921
public function __construct()
2022
{
21-
$this->functionMap = require 'phar://' . \Safe\FileCreator::getSafeRootDir() . '/generator/vendor/phpstan/phpstan/phpstan.phar/resources/functionMap.php';
22-
$this->customFunctionMap = require \Safe\FileCreator::getSafeRootDir() . '/generator/config/CustomPhpStanFunctionMap.php';
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';
2325
}
2426

2527
public function hasFunction(string $functionName): bool

generator/src/PhpStanFunctions/PhpStanParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Safe\PhpStanFunctions;
66

7-
use Safe\Type;
7+
use Safe\XmlDocParser\Type;
88

99
class PhpStanParameter
1010
{

0 commit comments

Comments
 (0)