From 0d6e73b3f1d08a26a5a8fb0ad56277d1140b311d Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 8 Dec 2021 19:30:15 +0100 Subject: [PATCH] small optimizations (#20) * small optimizations * minor fixes --- docker/docker-compose.yml => docker-compose.yml | 6 +++--- docker/.env | 1 - docker/dev/php/Dockerfile | 2 +- phpstan.neon | 1 + src/Merger/SchemaMerger.php | 15 +++++++-------- src/Optimizer/OptimizerInterface.php | 4 ++++ src/Optimizer/PrimitiveSchemaOptimizer.php | 1 + 7 files changed, 17 insertions(+), 13 deletions(-) rename docker/docker-compose.yml => docker-compose.yml (60%) delete mode 100644 docker/.env diff --git a/docker/docker-compose.yml b/docker-compose.yml similarity index 60% rename from docker/docker-compose.yml rename to docker-compose.yml index 49a8e1a..60a5517 100644 --- a/docker/docker-compose.yml +++ b/docker-compose.yml @@ -2,10 +2,10 @@ version: '3.2' services: php: build: - context: ./ - dockerfile: dev/php/Dockerfile + context: docker/dev + dockerfile: php/Dockerfile args: HOST_USER: ${USER} HOST_USER_ID: ${USER_ID} volumes: - - ../:/var/www/html + - ./:/var/www/html diff --git a/docker/.env b/docker/.env deleted file mode 100644 index eba8c5d..0000000 --- a/docker/.env +++ /dev/null @@ -1 +0,0 @@ -COMPOSE_PROJECT_NAME=php-avro-schema-generator diff --git a/docker/dev/php/Dockerfile b/docker/dev/php/Dockerfile index 8b68cdd..bd0b4d9 100644 --- a/docker/dev/php/Dockerfile +++ b/docker/dev/php/Dockerfile @@ -3,7 +3,7 @@ FROM php:7.4-cli-alpine3.15 ARG HOST_USER_ID ARG HOST_USER -COPY dev/php/files/bin/ /usr/local/bin/ +COPY php/files/bin/ /usr/local/bin/ # SYS: Install required packages RUN apk --no-cache upgrade && \ diff --git a/phpstan.neon b/phpstan.neon index 1a51413..2ee099c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,3 +6,4 @@ parameters: - "#Call to function token_get_all\\(\\) on a separate line has no effect.#" - "#Strict comparison using === between array&nonEmpty and ',' will always evaluate to false.#" - "#Strict comparison using === between array&nonEmpty and ';' will always evaluate to false.#" + - "#Method PhpKafka\\\\\\PhpAvroSchemaGenerator\\\\\\Optimizer\\\\\\OptimizerInterface::optimize\\(\\) invoked with 2 parameters, 1 required.#" diff --git a/src/Merger/SchemaMerger.php b/src/Merger/SchemaMerger.php index 26ab87c..bb83e84 100644 --- a/src/Merger/SchemaMerger.php +++ b/src/Merger/SchemaMerger.php @@ -4,11 +4,11 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Merger; +use AvroSchema; use AvroSchemaParseException; use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro; use PhpKafka\PhpAvroSchemaGenerator\Exception\SchemaMergerException; use PhpKafka\PhpAvroSchemaGenerator\Optimizer\OptimizerInterface; -use PhpKafka\PhpAvroSchemaGenerator\Optimizer\PrimitiveSchemaOptimizer; use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistryInterface; use PhpKafka\PhpAvroSchemaGenerator\Schema\SchemaTemplateInterface; @@ -65,7 +65,7 @@ public function getResolvedSchemaTemplate(SchemaTemplateInterface $rootSchemaTem $exceptionThrown = false; try { - \AvroSchema::parse($rootDefinition); + AvroSchema::parse($rootDefinition); } catch (AvroSchemaParseException $e) { if (false === strpos($e->getMessage(), ' is not a schema we know about.')) { throw $e; @@ -124,14 +124,13 @@ public function merge( foreach ($registry->getRootSchemas() as $rootSchemaTemplate) { try { $resolvedTemplate = $this->getResolvedSchemaTemplate($rootSchemaTemplate); + foreach ($this->optimizers as $optimizer) { $resolvedTemplate = $resolvedTemplate->withSchemaDefinition( - $optimizer instanceof PrimitiveSchemaOptimizer ? - $optimizer->optimize( - $resolvedTemplate->getSchemaDefinition(), - $resolvedTemplate->isPrimitive() - ) : - $optimizer->optimize($resolvedTemplate->getSchemaDefinition()) + $optimizer->optimize( + $resolvedTemplate->getSchemaDefinition(), + $resolvedTemplate->isPrimitive() + ) ); } } catch (SchemaMergerException $e) { diff --git a/src/Optimizer/OptimizerInterface.php b/src/Optimizer/OptimizerInterface.php index b09054e..74a6496 100644 --- a/src/Optimizer/OptimizerInterface.php +++ b/src/Optimizer/OptimizerInterface.php @@ -6,5 +6,9 @@ interface OptimizerInterface { + /** + * @param string $definition + * @return string + */ public function optimize(string $definition): string; } diff --git a/src/Optimizer/PrimitiveSchemaOptimizer.php b/src/Optimizer/PrimitiveSchemaOptimizer.php index c3c2190..4d8e1ec 100644 --- a/src/Optimizer/PrimitiveSchemaOptimizer.php +++ b/src/Optimizer/PrimitiveSchemaOptimizer.php @@ -8,6 +8,7 @@ class PrimitiveSchemaOptimizer extends AbstractOptimizer implements OptimizerInt { /** * @param string $definition + * @param bool $isPrimitive * @return string * @throws \JsonException */