From 9964566872feab99ec68b89271c6b7d1ee8ee71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Wed, 13 Dec 2023 12:53:03 +0100 Subject: [PATCH] Code: add types --- phpstan.neon | 1 + src/Analyser/Database/DatabaseAnalyser.php | 8 +- src/Config/Config.php | 67 ++++++-------- src/Config/Impl/SeparateConfig.php | 2 +- src/Config/Impl/TogetherConfig.php | 2 +- src/Entity/Column.php | 88 ++++++++----------- src/Entity/Database.php | 2 +- src/Entity/ForeignKey.php | 12 +-- src/Entity/PhpDoc.php | 38 ++++---- src/Entity/PhpRelDoc.php | 71 +++++++++------ src/Entity/Table.php | 12 ++- src/Generator.php | 31 +++---- src/Generator/AbstractGenerator.php | 9 +- .../Decorator/ColumnConstantGenerator.php | 8 +- .../Entity/Decorator/ColumnDocumentor.php | 27 ++---- src/Generator/Entity/EntityGenerator.php | 27 +++--- src/Generator/Facade/FacadeGenerator.php | 13 ++- src/Generator/Mapper/MapperGenerator.php | 15 ++-- src/Generator/Model/ModelGenerator.php | 12 ++- .../Repository/RepositoryGenerator.php | 18 ++-- src/Resolver/Impl/SimpleResolver.php | 35 ++++---- src/Resolver/Impl/SimpleSeparateResolver.php | 19 ++-- src/Resolver/Impl/SimpleTogetherResolver.php | 34 +++---- src/SimpleFactory.php | 6 +- src/Utils/DocBuilder.php | 5 +- src/Utils/Helpers.php | 7 +- 26 files changed, 258 insertions(+), 311 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 562ab05..5260d60 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -16,3 +16,4 @@ parameters: - .docs ignoreErrors: + - '#Construct empty\(\) is not allowed. Use more strict comparison\.#' diff --git a/src/Analyser/Database/DatabaseAnalyser.php b/src/Analyser/Database/DatabaseAnalyser.php index 5703761..94de272 100644 --- a/src/Analyser/Database/DatabaseAnalyser.php +++ b/src/Analyser/Database/DatabaseAnalyser.php @@ -16,11 +16,9 @@ class DatabaseAnalyser implements IAnalyser { - /** @var Connection */ - private $connection; + private Connection $connection; - /** @var Driver */ - private $driver; + private Driver $driver; public function __construct(string $dns, string $username, ?string $password = null) { @@ -69,7 +67,7 @@ protected function analyseColumns(Table $table): void $column->setNullable($col['nullable']); $column->setType(Helpers::columnType($col['nativetype'])); $column->setDefault($col['default']); - $column->setOnUpdate(Strings::contains($col['vendor']['extra'] ?? $col['vendor']['Extra'], 'on update')); + $column->setOnUpdate(str_contains($col['vendor']['extra'] ?? $col['vendor']['Extra'], 'on update')); // Analyse ENUM if ($col['nativetype'] === ColumnTypes::NATIVE_TYPE_ENUM) { diff --git a/src/Config/Config.php b/src/Config/Config.php index 7d0627c..8b851c3 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -18,7 +18,7 @@ class Config implements ArrayAccess public const STRATEGY_SEPARATE = 2; /** @var mixed[] */ - protected $defaults = [ + protected array $defaults = [ // Output folder 'output' => null, // 1 => Entity + Repository + Mapper + Facade => same folder (folder = table name) @@ -80,7 +80,7 @@ class Config implements ArrayAccess ]; /** @var mixed[] */ - protected $config = []; + protected array $config = []; /** * @param mixed[] $configuration @@ -88,51 +88,37 @@ class Config implements ArrayAccess public function __construct(array $configuration) { // Validate config - if ($extra = array_diff_key($configuration, $this->defaults)) { + if (($extra = array_diff_key($configuration, $this->defaults)) !== []) { $extra = implode(', ', array_keys($extra)); + throw new InvalidStateException('Unknown configuration option ' . $extra . '.'); } $this->config = array_merge($this->defaults, $configuration); } - /** - * MAGIC METHODS *********************************************************** - */ - - /** - * @return mixed - */ - public function __get(string $name) + public function get(string $name): mixed { return $this->offsetGet($name); } - /** - * @param string|mixed $name - * @param mixed $value - */ - public function __set($name, $value): void + public function getString(string $name): string { - $this->offsetSet($name, $value); + $ret = $this->offsetGet($name); + assert(is_string($ret)); + + return $ret; } - /** - * @return mixed - */ - public function get(string $name) + public function getBool(string $name): bool { - return $this->offsetGet($name); - } + $ret = $this->offsetGet($name); + assert(is_bool($ret)); - /** - * ARRAY ACCESS ************************************************************ - */ + return $ret; + } - /** - * @param mixed $offset - */ - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->config); } @@ -149,21 +135,24 @@ public function offsetGet(mixed $offset): mixed throw new InvalidStateException('Undefined offset: ' . $offset); } - /** - * @param string|mixed $offset - * @param mixed $value - */ - public function offsetSet($offset, $value): void + public function offsetSet(mixed $offset, mixed $value): void { $this->config[$offset] = $value; } - /** - * @param mixed $offset - */ - public function offsetUnset($offset): void + public function offsetUnset(mixed $offset): void { unset($this->config[$offset]); } + public function __get(string $name): mixed + { + return $this->offsetGet($name); + } + + public function __set(string $name, ?string $value): void + { + $this->offsetSet($name, $value); + } + } diff --git a/src/Config/Impl/SeparateConfig.php b/src/Config/Impl/SeparateConfig.php index 5aa3c9e..89b28cc 100644 --- a/src/Config/Impl/SeparateConfig.php +++ b/src/Config/Impl/SeparateConfig.php @@ -8,7 +8,7 @@ class SeparateConfig extends Config { /** @var mixed[] */ - protected $defaults = [ + protected array $defaults = [ // Output folder 'output' => null, // Generator diff --git a/src/Config/Impl/TogetherConfig.php b/src/Config/Impl/TogetherConfig.php index fd1f026..70ffdbd 100644 --- a/src/Config/Impl/TogetherConfig.php +++ b/src/Config/Impl/TogetherConfig.php @@ -8,7 +8,7 @@ class TogetherConfig extends Config { /** @var mixed[] */ - protected $defaults = [ + protected array $defaults = [ // Output folder 'output' => null, // Generator diff --git a/src/Entity/Column.php b/src/Entity/Column.php index 1912931..778b32f 100644 --- a/src/Entity/Column.php +++ b/src/Entity/Column.php @@ -8,48 +8,36 @@ class Column { - /** @var Table|null */ - private $table; + private ?Table $table = null; - /** @var string */ - private $name; + private ?string $name = null; - /** @var mixed */ - private $type; + private ?string $type = null; - /** @var mixed */ - private $subtype; + private ?string $subtype = null; - /** @var bool */ - private $nullable; + private ?bool $nullable = null; - /** @var mixed */ - private $default; + private ?string $default = null; /** @var string[] */ - private $enum = []; + private array $enum = []; - /** @var bool */ - private $onUpdate; + private bool $onUpdate; - /** @var PhpDoc|null */ - private $phpDoc; + private ?PhpDoc $phpDoc = null; - /** @var bool|null */ - private $primary; + private ?bool $primary = null; - /** @var bool */ - private $unique; + private bool $unique; - /** @var bool */ - private $index; + private bool $index; - /** @var ForeignKey */ - private $foreignKey; + private ForeignKey $foreignKey; public function attach(Table $table): void { - if ($this->table) { + if ($this->table !== null) { throw new InvalidAttachException('Column is already attached to table.'); } @@ -58,7 +46,7 @@ public function attach(Table $table): void public function getTable(): Table { - if (!$this->table) { + if ($this->table === null) { throw new LogicException('Table is needed'); } @@ -67,6 +55,10 @@ public function getTable(): Table public function getName(): string { + if ($this->name === null) { + throw new LogicException('Name is needed'); + } + return $this->name; } @@ -75,39 +67,35 @@ public function setName(string $name): void $this->name = $name; } - /** - * @return mixed - */ - public function getType() + public function getType(): string { + if ($this->type === null) { + throw new LogicException('Type is needed'); + } + return $this->type; } - /** - * @param mixed $type - */ - public function setType($type): void + public function setType(string $type): void { $this->type = $type; } - /** - * @return mixed - */ - public function getSubtype() + public function getSubtype(): string { + if ($this->subtype === null) { + throw new LogicException('Subtype is needed'); + } + return $this->subtype; } - /** - * @param mixed $subtype - */ - public function setSubtype($subtype): void + public function setSubtype(string $subtype): void { $this->subtype = $subtype; } - public function isNullable(): bool + public function isNullable(): ?bool { return $this->nullable; } @@ -117,18 +105,12 @@ public function setNullable(bool $nullable): void $this->nullable = $nullable; } - /** - * @return mixed - */ - public function getDefault() + public function getDefault(): ?string { return $this->default; } - /** - * @param mixed $default - */ - public function setDefault($default): void + public function setDefault(string $default): void { $this->default = $default; } @@ -161,7 +143,7 @@ public function setOnUpdate(bool $onUpdate): void public function getPhpDoc(): PhpDoc { - if (!$this->phpDoc) { + if ($this->phpDoc === null) { $this->phpDoc = new PhpDoc(); } diff --git a/src/Entity/Database.php b/src/Entity/Database.php index 819375d..ca93215 100644 --- a/src/Entity/Database.php +++ b/src/Entity/Database.php @@ -8,7 +8,7 @@ class Database { /** @var Table[] */ - private $tables = []; + private array $tables = []; /** * @return Table[] diff --git a/src/Entity/ForeignKey.php b/src/Entity/ForeignKey.php index 4c2ec11..386aaa7 100644 --- a/src/Entity/ForeignKey.php +++ b/src/Entity/ForeignKey.php @@ -5,17 +5,13 @@ class ForeignKey { - /** @var string */ - private $sourceTable; + private string $sourceTable; - /** @var string */ - private $sourceColumn; + private string $sourceColumn; - /** @var string */ - private $referenceTable; + private string $referenceTable; - /** @var string */ - private $referenceColumn; + private string $referenceColumn; public function getSourceTable(): string { diff --git a/src/Entity/PhpDoc.php b/src/Entity/PhpDoc.php index 28168e5..3915b58 100644 --- a/src/Entity/PhpDoc.php +++ b/src/Entity/PhpDoc.php @@ -7,29 +7,21 @@ class PhpDoc { - /** @var string */ - private $annotation; + private string $annotation; - /** @var string */ - private $type; + private string $type; - /** @var string */ - private $variable; + private string $variable; - /** @var string */ - private $enum; + private string $enum; - /** @var string */ - private $default; + private ?string $default; - /** @var bool */ - private $virtual; + private bool $virtual; - /** @var bool */ - private $primary; + private bool $primary; - /** @var PhpRelDoc|null */ - private $relation; + private ?PhpRelDoc $relation = null; public function getAnnotation(): string { @@ -71,12 +63,12 @@ public function setEnum(string $enum): void $this->enum = $enum; } - public function getDefault(): string + public function getDefault(): ?string { return $this->default; } - public function setDefault(string $default): void + public function setDefault(?string $default): void { $this->default = $default; } @@ -88,7 +80,7 @@ public function isVirtual(): bool public function setVirtual(bool $virtual): void { - $this->virtual = (bool) $virtual; + $this->virtual = $virtual; } public function getRelation(): ?PhpRelDoc @@ -116,7 +108,7 @@ public function __toString(): string $b = new DocBuilder(); // Anotation (@..) - if ($this->annotation) { + if ($this->annotation !== '') { $b->append($this->annotation); } else { if ($this->virtual) { @@ -133,12 +125,12 @@ public function __toString(): string $b->append(sprintf('$%s', $this->variable)); // Default - if ($this->default) { + if ($this->default !== '') { $b->append(sprintf('{default %s}', $this->default)); } // Enum {enum ..} - if ($this->enum) { + if ($this->enum !== '') { $b->append(sprintf('{enum self::%s_*}', $this->enum)); } @@ -152,7 +144,7 @@ public function __toString(): string } // Relation - if ($this->relation) { + if ($this->relation !== null) { $b->append(sprintf('{%s}', (string) $this->relation)); } diff --git a/src/Entity/PhpRelDoc.php b/src/Entity/PhpRelDoc.php index f094447..500c4e7 100644 --- a/src/Entity/PhpRelDoc.php +++ b/src/Entity/PhpRelDoc.php @@ -4,37 +4,32 @@ use Contributte\Nextras\Orm\Generator\Utils\DocBuilder; use InvalidArgumentException; +use LogicException; class PhpRelDoc { - /** - * Order directions - */ - public const ASC = 1; public const DESC = 2; - /** @var string */ - private $type; + private ?string $type; - /** @var string */ - private $entity; + private ?string $entity; - /** @var string */ - private $variable; + private ?string $variable; - /** @var bool */ - private $primary; + private ?bool $primary; - /** @var string */ - private $orderProperty; + private ?string $orderProperty; - /** @var int */ - private $orderDirection; + private ?int $orderDirection; public function getType(): string { + if ($this->type === null) { + throw new LogicException('Type is needed'); + } + return $this->type; } @@ -45,6 +40,10 @@ public function setType(string $type): void public function getEntity(): string { + if ($this->entity === null) { + throw new LogicException('Entity is needed'); + } + return $this->entity; } @@ -55,6 +54,10 @@ public function setEntity(string $entity): void public function getVariable(): string { + if ($this->variable === null) { + throw new LogicException('Variable is needed'); + } + return $this->variable; } @@ -65,16 +68,24 @@ public function setVariable(string $variable): void public function isPrimary(): bool { + if ($this->primary === null) { + throw new LogicException('Primary is needed'); + } + return $this->primary; } public function setPrimary(bool $primary): void { - $this->primary = (bool) $primary; + $this->primary = $primary; } public function getOrderProperty(): string { + if ($this->orderProperty === null) { + throw new LogicException('OrderProperty is needed'); + } + return $this->orderProperty; } @@ -85,12 +96,16 @@ public function setOrderProperty(string $orderProperty): void public function getOrderDirection(): int { + if ($this->orderDirection === null) { + throw new LogicException('OrderDirection is needed'); + } + return $this->orderDirection; } public function setOrderDirection(int $direction): void { - if (in_array($direction, [self::ASC, self::DESC])) { + if (in_array($direction, [self::ASC, self::DESC], true)) { throw new InvalidArgumentException('Unknown order direction ' . $direction); } @@ -102,30 +117,30 @@ public function __toString(): string $b = new DocBuilder(); // Type (1:m, m:1, m:n, etc..) - $b->append($this->type); + $b->append($this->getType()); // Entity and variable (Entity::$variable) - if ($this->variable) { - $b->str(ucfirst($this->entity)); + if ($this->variable !== null) { + $b->str(ucfirst($this->getEntity())); $b->str('::'); - $b->append('$' . $this->variable); + $b->append('$' . $this->getVariable()); } else { - $b->append(ucfirst($this->entity)); + $b->append(ucfirst($this->getEntity())); } // Primary - if ($this->primary) { + if ($this->primary !== null) { $b->append('primary'); } // Order (order:*property*) - if ($this->orderProperty) { - $b->append('order:' . $this->orderProperty); + if ($this->orderProperty !== null) { + $b->append('order:' . $this->getOrderProperty()); } // Ordering (DESC/ASC) - if ($this->orderDirection) { - $b->append($this->orderDirection === self::ASC ? 'ASC' : 'DESC'); + if ($this->orderDirection !== null) { + $b->append($this->getOrderDirection() === self::ASC ? 'ASC' : 'DESC'); } return (string) $b; diff --git a/src/Entity/Table.php b/src/Entity/Table.php index c096038..4814931 100644 --- a/src/Entity/Table.php +++ b/src/Entity/Table.php @@ -9,18 +9,16 @@ class Table { - /** @var Database|null */ - private $database; + private ?Database $database = null; - /** @var string */ - private $name; + private string $name; /** @var Column[] */ - private $columns = []; + private array $columns = []; public function attach(Database $database): void { - if ($this->database) { + if ($this->database !== null) { throw new InvalidAttachException('Table is already attached to database.'); } @@ -29,7 +27,7 @@ public function attach(Database $database): void public function getDatabase(): Database { - if (!$this->database) { + if ($this->database === null) { throw new LogicException('Database is needed'); } diff --git a/src/Generator.php b/src/Generator.php index 3a83c64..114522f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -9,26 +9,19 @@ class Generator { - /** @var Config */ - private $config; + private Config $config; - /** @var IAnalyser */ - private $analyser; + private IAnalyser $analyser; - /** @var IGenerator */ - private $entityGenerator; + private IGenerator $entityGenerator; - /** @var IGenerator */ - private $repositoryGenerator; + private IGenerator $repositoryGenerator; - /** @var IGenerator */ - private $mapperGenerator; + private IGenerator $mapperGenerator; - /** @var IGenerator */ - private $facadeGenerator; + private IGenerator $facadeGenerator; - /** @var IGenerator */ - private $modelGenerator; + private IGenerator $modelGenerator; public function __construct(Config $config, IAnalyser $analyser) { @@ -93,23 +86,23 @@ public function generate(): void { $database = $this->analyser->analyse(); - if ($this->config->get('generator.generate.entities')) { + if ($this->config->getBool('generator.generate.entities')) { $this->entityGenerator->generate($database); } - if ($this->config->get('generator.generate.repositories')) { + if ($this->config->getBool('generator.generate.repositories')) { $this->repositoryGenerator->generate($database); } - if ($this->config->get('generator.generate.mappers')) { + if ($this->config->getBool('generator.generate.mappers')) { $this->mapperGenerator->generate($database); } - if ($this->config->get('generator.generate.facades')) { + if ($this->config->getBool('generator.generate.facades')) { $this->facadeGenerator->generate($database); } - if ($this->config->get('generator.generate.model')) { + if ($this->config->getBool('generator.generate.model')) { $this->modelGenerator->generate($database); } } diff --git a/src/Generator/AbstractGenerator.php b/src/Generator/AbstractGenerator.php index 636b46a..5a824bc 100644 --- a/src/Generator/AbstractGenerator.php +++ b/src/Generator/AbstractGenerator.php @@ -9,22 +9,21 @@ abstract class AbstractGenerator implements IGenerator { - /** @var Config */ - protected $config; + protected Config $config; public function __construct(Config $config) { $this->config = $config; } + abstract public function generate(Database $database): void; + /** * Generate file */ protected function generateFile(string $filename, string $code): void { - FileSystem::write($this->config->get('output') . DIRECTORY_SEPARATOR . $filename, "config->getString('output') . DIRECTORY_SEPARATOR . $filename, "config = $config; } - public function doDecorate(Column $column, ClassType $class, PhpNamespace $namespace): void { - if (!$this->config->get('entity.generate.column.constant')) { + if ($this->config->getString('entity.generate.column.constant') === '') { return; } - $name = Strings::upper($this->config->get('entity.column.constants.prefix') . $column->getName()); + $name = Strings::upper($this->config->getString('entity.column.constants.prefix') . $column->getName()); $class->addConstant($name, Helpers::camelCase($column->getName())); } diff --git a/src/Generator/Entity/Decorator/ColumnDocumentor.php b/src/Generator/Entity/Decorator/ColumnDocumentor.php index 3b0c3b6..6e262a6 100644 --- a/src/Generator/Entity/Decorator/ColumnDocumentor.php +++ b/src/Generator/Entity/Decorator/ColumnDocumentor.php @@ -16,11 +16,9 @@ class ColumnDocumentor implements IDecorator { - /** @var IEntityResolver */ - private $resolver; + private IEntityResolver $resolver; - /** @var bool */ - private $generateRelations; + private bool $generateRelations; public function __construct(IEntityResolver $resolver, bool $generateRelations) { @@ -36,7 +34,7 @@ public function doDecorate(Column $column, ClassType $class, PhpNamespace $names $doc->setAnnotation('@property'); // Type - $doc->setType($this->getRealType($column) . ($column->isNullable() ? '|NULL' : '')); + $doc->setType($this->getRealType($column) . ($column->isNullable() === true ? '|NULL' : '')); // Variable $doc->setVariable(Helpers::camelCase($column->getName())); @@ -60,7 +58,7 @@ public function doDecorate(Column $column, ClassType $class, PhpNamespace $names $doc->setType($this->resolver->resolveEntityName($ftable)); $doc->setRelation($relDoc = new PhpRelDoc()); - if (($use = $this->getRealUse($ftable, $namespace))) { + if (($use = $this->getRealUse($ftable, $namespace)) !== null) { $namespace->addUse($use); } @@ -75,38 +73,27 @@ public function doDecorate(Column $column, ClassType $class, PhpNamespace $names $class->addComment((string) $column->getPhpDoc()); } - /** - * @return mixed - */ - protected function getRealType(Column $column) + protected function getRealType(Column $column): string { switch ($column->getType()) { case ColumnTypes::TYPE_ENUM: return $column->getSubtype(); - default: return $column->getType(); } } - /** - * @return mixed - */ - protected function getRealDefault(Column $column) + protected function getRealDefault(Column $column): ?string { switch ($column->getType()) { case ColumnTypes::TYPE_ENUM: return 'self::' . $column->getDefault(); - default: return $column->getDefault(); } } - /** - * @return mixed - */ - protected function getRealUse(Table $table, PhpNamespace $namespace) + protected function getRealUse(Table $table, PhpNamespace $namespace): ?string { if ($namespace->getName() === $this->resolver->resolveEntityNamespace($table)) { return null; diff --git a/src/Generator/Entity/EntityGenerator.php b/src/Generator/Entity/EntityGenerator.php index cdbd72d..7cd9de1 100644 --- a/src/Generator/Entity/EntityGenerator.php +++ b/src/Generator/Entity/EntityGenerator.php @@ -16,11 +16,10 @@ class EntityGenerator extends AbstractGenerator { - /** @var IEntityResolver */ - private $resolver; + private IEntityResolver $resolver; /** @var IDecorator[] */ - private $decorators = []; + private array $decorators = []; public function __construct(Config $config, IEntityResolver $resolver) { @@ -29,7 +28,7 @@ public function __construct(Config $config, IEntityResolver $resolver) $this->resolver = $resolver; $this->decorators[] = new ColumnMapper(); - $this->decorators[] = new ColumnDocumentor($resolver, $config->get('entity.generate.relations')); + $this->decorators[] = new ColumnDocumentor($resolver, $config->getBool('entity.generate.relations')); $this->decorators[] = new ColumnConstantGenerator($config); } @@ -41,8 +40,8 @@ public function generate(Database $database): void $class = $namespace->addClass($this->resolver->resolveEntityName($table)); // Detect extends class - if (($extends = $this->config->get('entity.extends')) === null) { - $extends = $this->config->get('nextras.orm.class.entity'); + if (($extends = $this->config->getString('entity.extends')) === '') { + $extends = $this->config->getString('nextras.orm.class.entity'); } // Add namespace and extends class @@ -51,8 +50,10 @@ public function generate(Database $database): void // Add table columns foreach ($table->getColumns() as $column) { - if ($this->config->get('generator.entity.exclude.primary')) { - if ($column->isPrimary()) continue; + if ($this->config->getBool('generator.entity.exclude.primary')) { + if ($column->isPrimary()) + + continue; } foreach ($this->decorators as $decorator) { @@ -65,19 +66,19 @@ public function generate(Database $database): void } // Generate abstract base class - if ($this->config->get('entity.extends') !== null) { + if ($this->config->getString('entity.extends') !== '') { // Create abstract class - $namespace = new PhpNamespace($this->config->get('entity.namespace')); - $class = $namespace->addClass(Helpers::extractShortName($this->config->get('entity.extends'))); + $namespace = new PhpNamespace($this->config->getString('entity.namespace')); + $class = $namespace->addClass(Helpers::extractShortName($this->config->getString('entity.extends'))); $class->setAbstract(); // Add extends from ORM/Entity - $extends = $this->config->get('nextras.orm.class.entity'); + $extends = $this->config->getString('nextras.orm.class.entity'); $namespace->addUse($extends); $class->setExtends($extends); // Save file - $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('entity.extends')), $this->config->get('entity.folder')), (string) $namespace); + $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->getString('entity.extends')), $this->config->getString('entity.folder')), (string) $namespace); } } diff --git a/src/Generator/Facade/FacadeGenerator.php b/src/Generator/Facade/FacadeGenerator.php index 0e64e9c..a7f4080 100644 --- a/src/Generator/Facade/FacadeGenerator.php +++ b/src/Generator/Facade/FacadeGenerator.php @@ -12,8 +12,7 @@ class FacadeGenerator extends AbstractGenerator { - /** @var IFacadeResolver */ - private $resolver; + private IFacadeResolver $resolver; public function __construct(Config $config, IFacadeResolver $resolver) { @@ -30,7 +29,7 @@ public function generate(Database $database): void $class = $namespace->addClass($this->resolver->resolveFacadeName($table)); // Detect extends class - if (($extends = $this->config->get('facade.extends')) !== null) { + if (($extends = $this->config->getString('facade.extends')) !== '') { $namespace->addUse($extends); $class->setExtends($extends); } @@ -40,14 +39,14 @@ public function generate(Database $database): void } // Generate abstract base class - if ($this->config->get('facade.extends') !== null) { + if ($this->config->getString('facade.extends') !== '') { // Create abstract class - $namespace = new PhpNamespace($this->config->get('facade.namespace')); - $class = $namespace->addClass(Helpers::extractShortName($this->config->get('facade.extends'))); + $namespace = new PhpNamespace($this->config->getString('facade.namespace')); + $class = $namespace->addClass(Helpers::extractShortName($this->config->getString('facade.extends'))); $class->setAbstract(true); // Save file - $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('facade.extends')), $this->config->get('facade.folder')), (string) $namespace); + $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->getString('facade.extends')), $this->config->getString('facade.folder')), (string) $namespace); } } diff --git a/src/Generator/Mapper/MapperGenerator.php b/src/Generator/Mapper/MapperGenerator.php index 85d4f7d..6502e4a 100644 --- a/src/Generator/Mapper/MapperGenerator.php +++ b/src/Generator/Mapper/MapperGenerator.php @@ -12,8 +12,7 @@ class MapperGenerator extends AbstractGenerator { - /** @var IMapperResolver */ - private $resolver; + private IMapperResolver $resolver; public function __construct(Config $config, IMapperResolver $resolver) { @@ -30,7 +29,7 @@ public function generate(Database $database): void $class = $namespace->addClass($this->resolver->resolveMapperName($table)); // Detect extends class - if (($extends = $this->config->get('mapper.extends')) !== null) { + if (($extends = $this->config->getString('mapper.extends')) !== '') { $namespace->addUse($extends); $class->setExtends($extends); } @@ -45,19 +44,19 @@ public function generate(Database $database): void } // Generate abstract base class - if ($this->config->get('mapper.extends') !== null) { + if ($this->config->getString('mapper.extends') !== '') { // Create abstract class - $namespace = new PhpNamespace($this->config->get('mapper.namespace')); - $class = $namespace->addClass(Helpers::extractShortName($this->config->get('mapper.extends'))); + $namespace = new PhpNamespace($this->config->getString('mapper.namespace')); + $class = $namespace->addClass(Helpers::extractShortName($this->config->getString('mapper.extends'))); $class->setAbstract(true); // Add extends from ORM/Mapper - $extends = $this->config->get('nextras.orm.class.mapper'); + $extends = $this->config->getString('nextras.orm.class.mapper'); $namespace->addUse($extends); $class->setExtends($extends); // Save file - $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('mapper.extends')), $this->config->get('mapper.folder')), (string) $namespace); + $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->getString('mapper.extends')), $this->config->getString('mapper.folder')), (string) $namespace); } } diff --git a/src/Generator/Model/ModelGenerator.php b/src/Generator/Model/ModelGenerator.php index 478d837..1b957fc 100644 --- a/src/Generator/Model/ModelGenerator.php +++ b/src/Generator/Model/ModelGenerator.php @@ -15,18 +15,16 @@ class ModelGenerator extends AbstractGenerator { - /** @var IModelResolver */ - private $modelResolver; + private IModelResolver $modelResolver; - /** @var IRepositoryResolver */ - private $repositoryResolver; + private IRepositoryResolver $repositoryResolver; - /** @var IEntityResolver */ - private $entityResolver; + private IEntityResolver $entityResolver; public function __construct(Config $config, IModelResolver $resolver, IRepositoryResolver $repositoryResolver, IEntityResolver $entityResolver) { parent::__construct($config); + $this->modelResolver = $resolver; $this->repositoryResolver = $repositoryResolver; $this->entityResolver = $entityResolver; @@ -35,7 +33,7 @@ public function __construct(Config $config, IModelResolver $resolver, IRepositor public function generate(Database $database): void { $namespace = new PhpNamespace($this->modelResolver->resolveModelNamespace()); - $nextrasModelClass = $this->config->get('nextras.orm.class.model'); + $nextrasModelClass = $this->config->getString('nextras.orm.class.model'); $namespace->addUse($nextrasModelClass); $class = $namespace->addClass($this->modelResolver->resolveModelName()); $class->setExtends($nextrasModelClass); diff --git a/src/Generator/Repository/RepositoryGenerator.php b/src/Generator/Repository/RepositoryGenerator.php index cb6b0b8..f26f79b 100644 --- a/src/Generator/Repository/RepositoryGenerator.php +++ b/src/Generator/Repository/RepositoryGenerator.php @@ -13,11 +13,9 @@ class RepositoryGenerator extends AbstractGenerator { - /** @var IRepositoryResolver */ - private $resolver; + private IRepositoryResolver $resolver; - /** @var IEntityResolver */ - private $entityResolver; + private IEntityResolver $entityResolver; public function __construct(Config $config, IRepositoryResolver $resolver, IEntityResolver $entityResolver) { @@ -35,7 +33,7 @@ public function generate(Database $database): void $class = $namespace->addClass($this->resolver->resolveRepositoryName($table)); // Detect extends class - if (($extends = $this->config->get('repository.extends')) !== null) { + if (($extends = $this->config->getString('repository.extends')) !== '') { $namespace->addUse($extends); $class->setExtends($extends); } @@ -53,19 +51,19 @@ public function generate(Database $database): void } // Generate abstract base class - if ($this->config->get('repository.extends') !== null) { + if ($this->config->getString('repository.extends') !== '') { // Create abstract class - $namespace = new PhpNamespace($this->config->get('repository.namespace')); - $class = $namespace->addClass(Helpers::extractShortName($this->config->get('repository.extends'))); + $namespace = new PhpNamespace($this->config->getString('repository.namespace')); + $class = $namespace->addClass(Helpers::extractShortName($this->config->getString('repository.extends'))); $class->setAbstract(true); // Add extends from ORM/Repository - $extends = $this->config->get('nextras.orm.class.repository'); + $extends = $this->config->getString('nextras.orm.class.repository'); $namespace->addUse($extends); $class->setExtends($extends); // Save file - $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('repository.extends')), $this->config->get('repository.folder')), (string) $namespace); + $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->getString('repository.extends')), $this->config->getString('repository.folder')), (string) $namespace); } } diff --git a/src/Resolver/Impl/SimpleResolver.php b/src/Resolver/Impl/SimpleResolver.php index dc3f8e7..5e6edeb 100644 --- a/src/Resolver/Impl/SimpleResolver.php +++ b/src/Resolver/Impl/SimpleResolver.php @@ -16,8 +16,7 @@ abstract class SimpleResolver implements IEntityResolver, IRepositoryResolver, IMapperResolver, IFacadeResolver, IModelResolver { - /** @var Config */ - protected $config; + protected Config $config; public function __construct(Config $config) { @@ -26,7 +25,22 @@ public function __construct(Config $config) public function resolveFilename(string $name, ?string $folder = null): string { - return ($folder ? $folder . Helpers::DS : null) . $this->normalize(ucfirst($name)) . '.' . IFilenameResolver::PHP_EXT; + return ($folder !== null ? $folder . Helpers::DS : null) . $this->normalize(ucfirst($name)) . '.' . IFilenameResolver::PHP_EXT; + } + + public function resolveModelName(): string + { + return $this->config->getString('model.name'); + } + + public function resolveModelNamespace(): string + { + return $this->config->getString('model.namespace'); + } + + public function resolveModelFilename(): string + { + return $this->config->getString('model.filename'); } protected function normalize(string $name): string @@ -51,19 +65,4 @@ protected function table(Table $table, bool $singularize = false): string return $name; } - public function resolveModelName(): string - { - return $this->config->get('model.name'); - } - - public function resolveModelNamespace(): string - { - return $this->config->get('model.namespace'); - } - - public function resolveModelFilename(): string - { - return $this->config->get('model.filename'); - } - } diff --git a/src/Resolver/Impl/SimpleSeparateResolver.php b/src/Resolver/Impl/SimpleSeparateResolver.php index 3074063..f92f2f9 100644 --- a/src/Resolver/Impl/SimpleSeparateResolver.php +++ b/src/Resolver/Impl/SimpleSeparateResolver.php @@ -16,7 +16,7 @@ public function resolveEntityName(Table $table): string public function resolveEntityNamespace(Table $table): string { - return $this->config->get('entity.namespace'); + return $this->config->getString('entity.namespace'); } public function resolveEntityFilename(Table $table): string @@ -31,7 +31,7 @@ public function resolveRepositoryName(Table $table): string public function resolveRepositoryNamespace(Table $table): string { - return $this->config->get('repository.namespace'); + return $this->config->getString('repository.namespace'); } public function resolveRepositoryFilename(Table $table): string @@ -51,7 +51,7 @@ public function resolveMapperFilename(Table $table): string public function resolveMapperNamespace(Table $table): string { - return $this->config->get('mapper.namespace'); + return $this->config->getString('mapper.namespace'); } public function resolveFacadeName(Table $table): string @@ -61,7 +61,7 @@ public function resolveFacadeName(Table $table): string public function resolveFacadeNamespace(Table $table): string { - return $this->config->get('facade.namespace'); + return $this->config->getString('facade.namespace'); } public function resolveFacadeFilename(Table $table): string @@ -75,22 +75,23 @@ public function resolveFacadeFilename(Table $table): string protected function resolveFilenameFor(string $type, Table $table): string { $name = $this->normalize(ucfirst($table->getName())); - if (!empty($this->config->get($type . '.name.singularize'))) { + if (!empty($this->config->getBool($type . '.name.singularize'))) { $name = InflectorFactory::create()->build()->singularize($name); } - $name .= $this->config->get($type . '.filename.suffix'); - return $this->config->get($type . '.folder') . DIRECTORY_SEPARATOR . $name . '.' . IFilenameResolver::PHP_EXT; + $name .= $this->config->getString($type . '.filename.suffix'); + + return $this->config->getString($type . '.folder') . DIRECTORY_SEPARATOR . $name . '.' . IFilenameResolver::PHP_EXT; } protected function resolveNameFor(string $type, Table $table): string { $name = $this->normalize(ucfirst($table->getName())); - if (!empty($this->config->get($type . '.name.singularize'))) { + if (!empty($this->config->getBool($type . '.name.singularize'))) { $name = InflectorFactory::create()->build()->singularize($name); } - return $name . $this->config->get($type . '.name.suffix'); + return $name . $this->config->getString($type . '.name.suffix'); } } diff --git a/src/Resolver/Impl/SimpleTogetherResolver.php b/src/Resolver/Impl/SimpleTogetherResolver.php index 32faa9e..8b38e6e 100644 --- a/src/Resolver/Impl/SimpleTogetherResolver.php +++ b/src/Resolver/Impl/SimpleTogetherResolver.php @@ -17,7 +17,7 @@ public function resolveEntityName(Table $table): string public function resolveEntityNamespace(Table $table): string { - return $this->config->get('orm.namespace') . Helpers::NS . $this->table($table, $this->config->get('orm.singularize')); + return $this->config->getString('orm.namespace') . Helpers::NS . $this->table($table, $this->config->getBool('orm.singularize')); } public function resolveEntityFilename(Table $table): string @@ -32,7 +32,7 @@ public function resolveRepositoryName(Table $table): string public function resolveRepositoryNamespace(Table $table): string { - return $this->config->get('orm.namespace') . Helpers::NS . $this->table($table); + return $this->config->getString('orm.namespace') . Helpers::NS . $this->table($table); } public function resolveRepositoryFilename(Table $table): string @@ -47,7 +47,7 @@ public function resolveMapperName(Table $table): string public function resolveMapperNamespace(Table $table): string { - return $this->config->get('orm.namespace') . Helpers::NS . $this->table($table, $this->config->get('orm.singularize')); + return $this->config->getString('orm.namespace') . Helpers::NS . $this->table($table, $this->config->getBool('orm.singularize')); } public function resolveMapperFilename(Table $table): string @@ -60,40 +60,42 @@ public function resolveFacadeName(Table $table): string return $this->resolveName('facade', $table); } + public function resolveFacadeNamespace(Table $table): string + { + return $this->config->getString('orm.namespace') . Helpers::NS . $this->table($table); + } + + public function resolveFacadeFilename(Table $table): string + { + return $this->resolveFilenameFor('facade', $table); + } + /** * @param string $type (entity,repository,mapper,facade) */ protected function resolveName(string $type, Table $table): string { $name = ucfirst($table->getName()); - if (!empty($this->config->get($type . '.name.singularize'))) { + if (!empty($this->config->getString($type . '.name.singularize'))) { $name = InflectorFactory::create()->build()->singularize($name); } $name .= $this->config->get($type . '.name.suffix'); + return $this->normalize($name); } protected function resolveFilenameFor(string $type, Table $table): string { - $folder = $this->table($table, $this->config->get('orm.singularize')); + $folder = $this->table($table, $this->config->getBool('orm.singularize')); $name = ucfirst($table->getName()); - if (!empty($this->config->get($type . '.name.singularize'))) { + if (!empty($this->config->getString($type . '.name.singularize'))) { $name = InflectorFactory::create()->build()->singularize($name); } $filename = $this->normalize($name . $this->config->get($type . '.filename.suffix')) . '.' . IFilenameResolver::PHP_EXT; - return $folder . Helpers::DS . $filename; - } - - public function resolveFacadeNamespace(Table $table): string - { - return $this->config->get('orm.namespace') . Helpers::NS . $this->table($table); - } - public function resolveFacadeFilename(Table $table): string - { - return $this->resolveFilenameFor('facade', $table); + return $folder . Helpers::DS . $filename; } } diff --git a/src/SimpleFactory.php b/src/SimpleFactory.php index 6221c6b..5033637 100644 --- a/src/SimpleFactory.php +++ b/src/SimpleFactory.php @@ -16,11 +16,9 @@ final class SimpleFactory { - /** @var Config */ - private $config; + private Config $config; - /** @var IAnalyser */ - private $analyser; + private IAnalyser $analyser; public function __construct(Config $config, IAnalyser $analyser) { diff --git a/src/Utils/DocBuilder.php b/src/Utils/DocBuilder.php index b795ab3..5ca9541 100644 --- a/src/Utils/DocBuilder.php +++ b/src/Utils/DocBuilder.php @@ -6,24 +6,27 @@ class DocBuilder { /** @var string[] */ - private $builder = []; + private array $builder = []; public function append(string $str): self { $this->str($str); $this->space(); + return $this; } public function str(string $str): self { $this->builder[] = $str; + return $this; } public function space(): self { $this->builder[] = ' '; + return $this; } diff --git a/src/Utils/Helpers.php b/src/Utils/Helpers.php index 91b3f74..d1c2621 100644 --- a/src/Utils/Helpers.php +++ b/src/Utils/Helpers.php @@ -19,7 +19,7 @@ class Helpers public const DS = DIRECTORY_SEPARATOR; /** @var string[] */ - public static $typePatterns = [ + public static array $typePatterns = [ '^_' => ColumnTypes::TYPE_TEXT, // PostgreSQL arrays 'BYTEA|BLOB|BIN' => ColumnTypes::TYPE_TEXT, 'TEXT|CHAR|POINT|INTERVAL' => ColumnTypes::TYPE_TEXT, @@ -32,7 +32,7 @@ class Helpers ]; /** @var string[] */ - public static $mnDelimiters = [ + public static array $mnDelimiters = [ '_to_', '_has_', '_x_', @@ -45,6 +45,7 @@ public static function camelCase(string $s): string $s = (string) preg_replace('#[_-](?=[a-z])#', ' ', $s); $s = substr(ucwords('x' . $s), 1); $s = str_replace(' ', '', $s); + return $s; } @@ -54,7 +55,7 @@ public static function columnType(string $type): string if (!isset($cache[$type])) { $cache[$type] = 'string'; foreach (self::$typePatterns as $s => $val) { - if (preg_match('#' . $s . '#i', $type)) { + if (preg_match('#' . $s . '#i', $type) !== false) { return $cache[$type] = $val; } }