Skip to content

Commit 8b3ed64

Browse files
authored
refactor token parser (#34)
* refactor token parser * save work * save work * save work * save work * save work * save work * save work * update readme * save work * save work * save work * save work * remove old token parser * update cc * save work * save work * fix tests * save work * add tests * up cov * save work * save work * save work * stan * exclude new parser * increase func max
1 parent 0826353 commit 8b3ed64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1839
-717
lines changed

.codeclimate.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ checks:
44
argument-count:
55
enabled: true
66
config:
7-
threshold: 4
7+
threshold: 5
88
complex-logic:
99
enabled: true
1010
config:
@@ -24,15 +24,15 @@ checks:
2424
method-lines:
2525
enabled: true
2626
config:
27-
threshold: 25
27+
threshold: 30
2828
nested-control-flow:
2929
enabled: true
3030
config:
3131
threshold: 4
3232
return-statements:
3333
enabled: true
3434
config:
35-
threshold: 4
35+
threshold: 5
3636
similar-code:
3737
enabled: true
3838
config:
@@ -42,8 +42,8 @@ checks:
4242
config:
4343
threshold: #language-specific defaults. overrides affect all languages.
4444
exclude_patterns:
45-
- "src/Parser/TokenParser.php"
4645
- "src/Command"
46+
- "src/Parser/ClassParser.php"
4747
- "tests/"
4848
- "**/vendor/"
4949
- "example/"

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Avro schema generator for PHP
22
[![Actions Status](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)
33
[![Maintainability](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/maintainability)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability)
4-
[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)
4+
[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)
5+
![Supported PHP versions: 7.4 .. 8.x](https://img.shields.io/badge/php-7.4%20..%208.x-blue.svg)
56
[![Latest Stable Version](https://poser.pugx.org/php-kafka/php-avro-schema-generator/v/stable)](https://packagist.org/packages/php-kafka/php-avro-schema-generator)
67

78
## Installation

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"require": {
1111
"ext-json": "*",
1212
"flix-tech/avro-php": "^3.0|^4.0",
13-
"symfony/console": "^4.3|^5.1"
13+
"symfony/console": "^4.3|^5.1",
14+
"nikic/php-parser": "^4.13",
15+
"pimple/pimple": "^3.5"
1416
},
1517
"require-dev": {
1618
"friendsofphp/php-cs-fixer": "^2.15",

example/classes/SomeBaseClass.php

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

99
abstract class SomeBaseClass
1010
{
11-
1211
/**
1312
* @var Wonderland
1413
*/

phpstan.neon

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
parameters:
22
level: 8
33
paths: [ src ]
4-
checkGenericClassInNonGenericObjectType: false
5-
ignoreErrors:
6-
- "#Call to function token_get_all\\(\\) on a separate line has no effect.#"
7-
- "#Strict comparison using === between non-empty-array<int, mixed> and ',' will always evaluate to false.#"
8-
- "#Strict comparison using === between non-empty-array<int, mixed> and ';' will always evaluate to false.#"
4+
checkGenericClassInNonGenericObjectType: false

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</include>
77
<exclude>
88
<directory>src/Command</directory>
9+
<file>src/AppContainer.php</file>
910
</exclude>
1011
<report>
1112
<clover outputFile="build/logs/phpunit/clover.xml"/>

src/AppContainer.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpKafka\PhpAvroSchemaGenerator;
6+
7+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\CommandServiceProvider;
8+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\ConverterServiceProvider;
9+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\GeneratorServiceProvider;
10+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\MergerServiceProvider;
11+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\ParserServiceProvider;
12+
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\RegistryServiceProvider;
13+
use Pimple\Container;
14+
15+
class AppContainer
16+
{
17+
/**
18+
* @return Container
19+
*/
20+
public static function init(): Container
21+
{
22+
$container = new Container();
23+
24+
$container
25+
->register(new GeneratorServiceProvider())
26+
->register(new MergerServiceProvider())
27+
->register(new ParserServiceProvider())
28+
->register(new ConverterServiceProvider())
29+
->register(new RegistryServiceProvider())
30+
->register(new CommandServiceProvider());
31+
32+
33+
return $container;
34+
}
35+
}

src/Avro/Avro.php

+20
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,24 @@ class Avro
2020
'map' => self::LONELIEST_NUMBER,
2121
'fixed' => self::LONELIEST_NUMBER,
2222
];
23+
24+
/**
25+
* @var string[]
26+
*/
27+
public const MAPPED_TYPES = array(
28+
'null' => 'null',
29+
'bool' => 'boolean',
30+
'boolean' => 'boolean',
31+
'string' => 'string',
32+
'int' => 'int',
33+
'integer' => 'int',
34+
'float' => 'float',
35+
'double' => 'double',
36+
'array' => 'array',
37+
'object' => 'object',
38+
'callable' => 'callable',
39+
'resource' => 'resource',
40+
'mixed' => 'mixed',
41+
'Collection' => 'array',
42+
);
2343
}

src/Command/SchemaGenerateCommand.php

+20-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,29 @@
55
namespace PhpKafka\PhpAvroSchemaGenerator\Command;
66

77
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator;
8+
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGeneratorInterface;
89
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry;
10+
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
911
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Input\InputArgument;
1113
use Symfony\Component\Console\Input\InputInterface;
1214
use Symfony\Component\Console\Output\OutputInterface;
1315

1416
class SchemaGenerateCommand extends Command
1517
{
18+
private SchemaGeneratorInterface $generator;
19+
private ClassRegistryInterface $classRegistry;
20+
21+
public function __construct(
22+
ClassRegistryInterface $classRegistry,
23+
SchemaGeneratorInterface $generator,
24+
string $name = null
25+
) {
26+
$this->classRegistry = $classRegistry;
27+
$this->generator = $generator;
28+
parent::__construct($name);
29+
}
30+
1631
protected function configure(): void
1732
{
1833
$this
@@ -36,15 +51,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
3651
$classDirectory = $this->getPath($classDirectoryArg);
3752
$outputDirectory = $this->getPath($outputDirectoryArg);
3853

39-
$registry = (new ClassRegistry())
40-
->addClassDirectory($classDirectory)
41-
->load();
42-
43-
$generator = new SchemaGenerator($registry, $outputDirectory);
44-
45-
$schemas = $generator->generate();
54+
$registry = $this->classRegistry->addClassDirectory($classDirectory)->load();
55+
$this->generator->setOutputDirectory($outputDirectory);
56+
$this->generator->setClassRegistry($registry);
4657

47-
$result = $generator->exportSchemas($schemas);
58+
$schemas = $this->generator->generate();
59+
$result = $this->generator->exportSchemas($schemas);
4860

4961
// retrieve the argument value using getArgument()
5062
$output->writeln(sprintf('Generated %d schema files', $result));

src/Command/SubSchemaMergeCommand.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\Command;
66

7+
use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMergerInterface;
78
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FieldOrderOptimizer;
89
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FullNameOptimizer;
910
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\OptimizerInterface;
1011
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\PrimitiveSchemaOptimizer;
1112
use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
1213
use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMerger;
14+
use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistryInterface;
1315
use Symfony\Component\Console\Command\Command;
1416
use Symfony\Component\Console\Input\InputArgument;
1517
use Symfony\Component\Console\Input\InputInterface;
@@ -18,12 +20,26 @@
1820

1921
class SubSchemaMergeCommand extends Command
2022
{
23+
private SchemaRegistryInterface $schemaRegistry;
24+
private SchemaMergerInterface $schemaMerger;
25+
2126
/** @var string[] */
22-
protected $optimizerOptionMapping = [
27+
protected array $optimizerOptionMapping = [
2328
'optimizeFieldOrder' => FieldOrderOptimizer::class,
2429
'optimizeFullNames' => FullNameOptimizer::class,
2530
'optimizePrimitiveSchemas' => PrimitiveSchemaOptimizer::class,
2631
];
32+
33+
public function __construct(
34+
SchemaMergerInterface $schemaMerger,
35+
SchemaRegistryInterface $schemaRegistry,
36+
string $name = null
37+
) {
38+
$this->schemaMerger = $schemaMerger;
39+
$this->schemaRegistry = $schemaRegistry;
40+
parent::__construct($name);
41+
}
42+
2743
protected function configure(): void
2844
{
2945
$this
@@ -71,20 +87,19 @@ public function execute(InputInterface $input, OutputInterface $output): int
7187
$templateDirectory = $this->getPath($templateDirectoryArg);
7288
$outputDirectory = $this->getPath($outputDirectoryArg);
7389

74-
$registry = (new SchemaRegistry())
75-
->addSchemaTemplateDirectory($templateDirectory)
76-
->load();
90+
$registry = $this->schemaRegistry->addSchemaTemplateDirectory($templateDirectory)->load();
7791

78-
$merger = new SchemaMerger($registry, $outputDirectory);
92+
$this->schemaMerger->setSchemaRegistry($registry);
93+
$this->schemaMerger->setOutputDirectory($outputDirectory);
7994

8095
/** @var OptimizerInterface $optimizerClass */
8196
foreach ($this->optimizerOptionMapping as $optionName => $optimizerClass) {
8297
if (true === (bool) $input->getOption($optionName)) {
83-
$merger->addOptimizer(new $optimizerClass());
98+
$this->schemaMerger->addOptimizer(new $optimizerClass());
8499
}
85100
}
86101

87-
$result = $merger->merge(
102+
$result = $this->schemaMerger->merge(
88103
(bool) $input->getOption('prefixWithNamespace'),
89104
(bool) $input->getOption('useFilenameAsSchemaName')
90105
);

0 commit comments

Comments
 (0)