From bc7fd3bf4f50553e929e5d72a7d8d8588fb524fc Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Sun, 14 Apr 2024 18:11:25 +0200 Subject: [PATCH] Formatting --- .php-cs-fixer.php | 42 +-------- src/Abstracts/AbstractCollection.php | 2 + src/Abstracts/AbstractModel.php | 24 ++--- src/Abstracts/AbstractService.php | 30 +++---- src/Abstracts/AbstractTableGateway.php | 61 ++++++------- src/Adapter.php | 4 +- src/Components/Column.php | 28 ++---- src/Components/Entity.php | 20 +++-- src/Components/Model.php | 63 ++++++------- src/Components/RelatedModel.php | 43 ++++----- src/Connection/Database.php | 30 +++---- src/Connection/Databases.php | 4 +- src/Entities/AbstractEntity.php | 6 +- src/Entities/Column.php | 2 + src/Entities/Table.php | 4 +- src/Exception/BenzineOrmException.php | 6 +- src/Exception/CollectionException.php | 6 +- .../DBTypeNotTranslatedException.php | 6 +- src/Exception/SchemaToAdaptorException.php | 6 +- src/Finder.php | 10 +-- src/Interfaces/CollectionsInterface.php | 2 + src/Interfaces/ModelInterface.php | 2 + src/Interfaces/QueryStatisticInterface.php | 2 + src/Interfaces/ServiceInterface.php | 10 ++- src/Interfaces/TableGatewayInterface.php | 6 +- src/Laminator.php | 90 ++++++++++--------- src/LaminatorSql.php | 5 +- src/Migrations/AbstractMigration.php | 4 +- src/Migrations/AbstractSeed.php | 4 +- src/Profiler/Profiler.php | 26 +++--- src/Profiler/QueryStatistic.php | 4 +- src/TabularData/Table.php | 8 +- src/TabularData/TableRow.php | 4 +- src/Transaction.php | 2 + 34 files changed, 267 insertions(+), 299 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index a68fdb1..4bf2dfb 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -1,42 +1,2 @@ in($directory); - } -} - -return PhpCsFixer\Config::create() - ->setRiskyAllowed(true) - ->setHideProgress(false) - ->setRules([ - '@PSR2' => true, - 'strict_param' => true, - 'array_syntax' => ['syntax' => 'short'], - '@PhpCsFixer' => true, - '@PHP73Migration' => true, - 'no_php4_constructor' => true, - 'no_unused_imports' => true, - 'no_useless_else' => true, - 'no_superfluous_phpdoc_tags' => false, - 'void_return' => true, - 'yoda_style' => false, - ]) - ->setFinder($finder) - ; +return require("vendor/benzine/core/.php-cs-fixer.php"); \ No newline at end of file diff --git a/src/Abstracts/AbstractCollection.php b/src/Abstracts/AbstractCollection.php index 56bd54d..a9f6731 100644 --- a/src/Abstracts/AbstractCollection.php +++ b/src/Abstracts/AbstractCollection.php @@ -1,5 +1,7 @@ getListOfProperties() as $property) { - $getFunction = "get{$property}"; - $currentValue = $this->{$getFunction}(); + $getFunction = "get{$property}"; + $currentValue = $this->{$getFunction}(); $array[$transformer->transform($property)] = $currentValue; } @@ -146,7 +146,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable { $primaryKeyValues = []; foreach ($this->_primary_keys as $internalName => $dbName) { - $getFunction = "get{$internalName}"; + $getFunction = "get{$internalName}"; $primaryKeyValues[$internalName] = $this->{$getFunction}(); } @@ -157,7 +157,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable { $primaryKeyValues = []; foreach ($this->_primary_keys as $internalName => $dbName) { - $getFunction = "get{$internalName}"; + $getFunction = "get{$internalName}"; $primaryKeyValues[$dbName] = $this->{$getFunction}(); } @@ -173,7 +173,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable { $autoIncrementKeyValues = []; foreach ($this->_autoincrement_keys as $autoincrement_key => $autoincrement_db_column) { - $getFunction = "get{$autoincrement_key}"; + $getFunction = "get{$autoincrement_key}"; $autoIncrementKeyValues[$autoincrement_key] = $this->{$getFunction}(); } @@ -209,7 +209,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable */ public function getListOfDirtyProperties(): array { - $transformer = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); + $transformer = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); $dirtyProperties = []; foreach ($this->getListOfProperties() as $property) { $originalProperty = $transformer->transform($property); @@ -217,7 +217,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable if (!isset($this->_original[$originalProperty]) || $this->{$property} != $this->_original[$originalProperty]) { $dirtyProperties[$property] = [ 'before' => $this->_original[$originalProperty] ?? null, - 'after' => $this->{$property}, + 'after' => $this->{$property}, ]; } } @@ -265,7 +265,7 @@ abstract class AbstractModel implements ModelInterface, \Serializable if (method_exists($this, 'getName')) { return $this->getName(); } - $labelParts = []; + $labelParts = []; $primaryKeyFields = array_keys($this->getPrimaryKeys()); foreach ($primaryKeyFields as $primaryKeyField) { $labelParts[] = $this->__get($primaryKeyField); diff --git a/src/Abstracts/AbstractService.php b/src/Abstracts/AbstractService.php index 8d8cee6..4668d73 100644 --- a/src/Abstracts/AbstractService.php +++ b/src/Abstracts/AbstractService.php @@ -1,5 +1,7 @@ getNewTableGatewayInstance(); + $tableGateway = $this->getNewTableGatewayInstance(); [$matches, $count] = $tableGateway->fetchAll( $limit, $offset, $wheres, $order, - null !== $orderDirection ? $orderDirection : Sql\Select::ORDER_ASCENDING + null !== $orderDirection ? $orderDirection : Select::ORDER_ASCENDING ); $collection = $this->getNewCollectionInstance(); @@ -51,15 +51,13 @@ abstract class AbstractService /** * @param null|string $distinctColumn * @param null|array|\Closure[] $wheres - * - * @return AbstractCollection */ public function getDistinct( string $distinctColumn, - array $wheres = null + ?array $wheres = null ): AbstractCollection { /** @var AbstractTableGateway $tableGateway */ - $tableGateway = $this->getNewTableGatewayInstance(); + $tableGateway = $this->getNewTableGatewayInstance(); [$matches, $count] = $tableGateway->fetchDistinct( $distinctColumn, $wheres @@ -76,11 +74,9 @@ abstract class AbstractService /** * @param null|array|\Closure[] $wheres - * - * @return int */ public function countAll( - array $wheres = null + ?array $wheres = null ): int { /** @var AbstractTableGateway $tableGateway */ $tableGateway = $this->getNewTableGatewayInstance(); @@ -91,7 +87,7 @@ abstract class AbstractService /** * @return Benzine\ORM\Abstracts\Model[] */ - public function search(Sql\Where $where, int $limit = null, int $offset = null): \Generator + public function search(Sql\Where $where, ?int $limit = null, ?int $offset = null): \Generator { $tableGateway = $this->getNewTableGatewayInstance(); @@ -118,7 +114,7 @@ abstract class AbstractService abstract public function getByField(string $field, $value, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?AbstractModel; - abstract public function getManyByField(string $field, $value, int $limit = null, int $offset = null, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): AbstractCollection; + abstract public function getManyByField(string $field, $value, ?int $limit = null, ?int $offset = null, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): AbstractCollection; abstract public function countByField(string $field, $value): int; diff --git a/src/Abstracts/AbstractTableGateway.php b/src/Abstracts/AbstractTableGateway.php index b83ea47..b011f0e 100644 --- a/src/Abstracts/AbstractTableGateway.php +++ b/src/Abstracts/AbstractTableGateway.php @@ -1,5 +1,7 @@ adapter = $adapter; - $this->table = $table; + $this->table = $table; if (!$sql) { $sql = new LaminatorSql($this->adapter, $this->table); @@ -91,9 +94,9 @@ abstract class AbstractTableGateway extends TableGateway return $updatedModel; } catch (InvalidQueryException $iqe) { throw new InvalidQueryException( - 'While trying to call '.get_class().'->save(): ... '. - $iqe->getMessage()."\n\n". - substr(var_export($model, true), 0, 1024)."\n\n", + 'While trying to call ' . get_class() . '->save(): ... ' . + $iqe->getMessage() . "\n\n" . + substr(var_export($model, true), 0, 1024) . "\n\n", $iqe->getCode(), $iqe ); @@ -188,6 +191,7 @@ abstract class AbstractTableGateway extends TableGateway public function update($data, $where = null, $oldData = []) { $data = array_filter($data); + // !\Kint::dump($data, $oldData, $where);exit; return parent::update($data, $where); } @@ -204,9 +208,9 @@ abstract class AbstractTableGateway extends TableGateway * @return array [ResultSet,int] Returns an array of resultSet,total_found_rows */ public function fetchAll( - int $limit = null, - int $offset = null, - array $wheres = null, + ?int $limit = null, + ?int $offset = null, + ?array $wheres = null, $order = null, string $direction = Select::ORDER_ASCENDING ) { @@ -326,7 +330,7 @@ abstract class AbstractTableGateway extends TableGateway */ public function fetchDistinct( string $distinctColumn, - array $wheres = null + ?array $wheres = null ) { /** @var Select $select */ $select = $this->getSql()->select(); @@ -403,9 +407,9 @@ abstract class AbstractTableGateway extends TableGateway } /** - * @throws BenzineException - * * @return null|ModelInterface + * + * @throws BenzineException */ public function fetchRandom() { @@ -489,7 +493,7 @@ abstract class AbstractTableGateway extends TableGateway public function getCountUnique(string $field, $wheres = []): int { $select = $this->getSql()->select(); - $select->columns(['total' => new Expression('DISTINCT '.$field)]); + $select->columns(['total' => new Expression('DISTINCT ' . $field)]); if (count($wheres) > 0) { foreach ($wheres as $where) { $select->where($where); @@ -536,7 +540,7 @@ abstract class AbstractTableGateway extends TableGateway ->current() ; - $highestPrimaryKey = !is_null($row) ? $row['max'] : 0; + $highestPrimaryKey = !is_null($row) ? $row['max'] : 0; $highestPrimaryKeys[$primaryKey] = $highestPrimaryKey; } @@ -558,7 +562,7 @@ abstract class AbstractTableGateway extends TableGateway ->current() ; - $highestAutoIncrementKey = !is_null($row) ? $row['max'] : 0; + $highestAutoIncrementKey = !is_null($row) ? $row['max'] : 0; $highestAutoIncrementKeys[$autoIncrementKey] = $highestAutoIncrementKey; } @@ -566,8 +570,6 @@ abstract class AbstractTableGateway extends TableGateway } /** - * @param $id - * * @return null|AbstractModel */ public function getById($id) @@ -576,9 +578,7 @@ abstract class AbstractTableGateway extends TableGateway } /** - * @param $field - * @param $value - * @param $orderBy string Field to sort by + * @param $orderBy string Field to sort by * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) * * @return null|array|\ArrayObject @@ -633,15 +633,11 @@ abstract class AbstractTableGateway extends TableGateway } /** - * @param Where $where - * @param null|int $limit - * @param null|int $offset * @param null|Expression|string $orderBy - * @param string $orderDirection * - * @return \Laminas\Db\ResultSet\ResultSetInterface + * @return ResultSetInterface */ - public function getManyByWhere(Where $where, int $limit = null, int $offset = null, $orderBy = null, string $orderDirection = Select::ORDER_ASCENDING) + public function getManyByWhere(Where $where, ?int $limit = null, ?int $offset = null, $orderBy = null, string $orderDirection = Select::ORDER_ASCENDING) { $select = $this->sql->select(); @@ -667,15 +663,14 @@ abstract class AbstractTableGateway extends TableGateway } /** - * @param null|int $limit int - * @param null|int $offset int - * @param null|string $orderBy string Field to sort by - * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) - * @param mixed $value + * @param null|int $limit int + * @param null|int $offset int + * @param null|string $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) * * @return AbstractCollection */ - public function getManyByField(string $field, $value, int $limit = null, int $offset = null, string $orderBy = null, string $orderDirection = Select::ORDER_ASCENDING) + public function getManyByField(string $field, $value, ?int $limit = null, ?int $offset = null, ?string $orderBy = null, string $orderDirection = Select::ORDER_ASCENDING) { if ($value instanceof \DateTime) { $value = $value->format('Y-m-d H:i:s'); @@ -695,7 +690,7 @@ abstract class AbstractTableGateway extends TableGateway new Expression('COUNT(*) as count'), ]); $statement = $this->sql->prepareStatementForSqlObject($select); - $result = $statement->execute(); + $result = $statement->execute(); $data = $result->current(); @@ -744,7 +739,7 @@ abstract class AbstractTableGateway extends TableGateway public function getBySelect(Select $select): array { $resultSet = $this->executeSelect($select); - $return = []; + $return = []; foreach ($resultSet as $result) { $return[] = $result; } @@ -758,7 +753,7 @@ abstract class AbstractTableGateway extends TableGateway public function getBySelectRaw(Select $select): array { $resultSet = $this->executeSelect($select); - $return = []; + $return = []; while ($result = $resultSet->getDataSource()->current()) { $return[] = $result; $resultSet->getDataSource()->next(); diff --git a/src/Adapter.php b/src/Adapter.php index 37b1b5e..c17a60b 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -1,5 +1,7 @@ setPhpType('\\'.DateTime::class); + $this->setPhpType('\\' . DateTime::class); break; @@ -342,8 +328,6 @@ class Column extends Entity } /** - * @param mixed $permittedValues - * * @return Column */ public function setPermittedValues($permittedValues) diff --git a/src/Components/Entity.php b/src/Components/Entity.php index 9f7fdf6..479bb0b 100644 --- a/src/Components/Entity.php +++ b/src/Components/Entity.php @@ -1,5 +1,7 @@ transSnake2Studly = new CaseTransformer(new Format\SnakeCase(), new Format\StudlyCaps()); - $this->transStudly2Camel = new CaseTransformer(new Format\StudlyCaps(), new Format\CamelCase()); - $this->transStudly2Studly = new CaseTransformer(new Format\StudlyCaps(), new Format\StudlyCaps()); - $this->transCamel2Camel = new CaseTransformer(new Format\CamelCase(), new Format\CamelCase()); - $this->transCamel2Studly = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); - $this->transSnake2Camel = new CaseTransformer(new Format\SnakeCase(), new Format\CamelCase()); - $this->transSnake2Spinal = new CaseTransformer(new Format\SnakeCase(), new Format\SpinalCase()); - $this->transCamel2Snake = new CaseTransformer(new Format\CamelCase(), new Format\SnakeCase()); + $this->transSnake2Studly = new CaseTransformer(new Format\SnakeCase(), new Format\StudlyCaps()); + $this->transStudly2Camel = new CaseTransformer(new Format\StudlyCaps(), new Format\CamelCase()); + $this->transStudly2Studly = new CaseTransformer(new Format\StudlyCaps(), new Format\StudlyCaps()); + $this->transCamel2Camel = new CaseTransformer(new Format\CamelCase(), new Format\CamelCase()); + $this->transCamel2Studly = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); + $this->transSnake2Camel = new CaseTransformer(new Format\SnakeCase(), new Format\CamelCase()); + $this->transSnake2Spinal = new CaseTransformer(new Format\SnakeCase(), new Format\SpinalCase()); + $this->transCamel2Snake = new CaseTransformer(new Format\CamelCase(), new Format\SnakeCase()); $this->transCamel2ScreamingSnake = new CaseTransformer(new Format\CamelCase(), new Format\ScreamingSnakeCase()); - $this->transStudly2Snake = new CaseTransformer(new Format\StudlyCaps(), new Format\SnakeCase()); + $this->transStudly2Snake = new CaseTransformer(new Format\StudlyCaps(), new Format\SnakeCase()); $this->transField2Property = $this->transCamel2Camel; } diff --git a/src/Components/Model.php b/src/Components/Model.php index 690c048..d20b77d 100644 --- a/src/Components/Model.php +++ b/src/Components/Model.php @@ -1,5 +1,7 @@ getType()) { // \Kint::dump($this->getTable(), $this->getClassPrefix(), $zendConstraint->getTableName()); - $keyMapIdLocal = $zendConstraint->getSchemaName().'::'.$zendConstraint->getTableName(); - $keyMapIdRemote = $zendConstraint->getReferencedTableSchema().'::'.$zendConstraint->getReferencedTableName(); - $localRelatedModel = $models[$keyMap[$keyMapIdLocal]]; + $keyMapIdLocal = $zendConstraint->getSchemaName() . '::' . $zendConstraint->getTableName(); + $keyMapIdRemote = $zendConstraint->getReferencedTableSchema() . '::' . $zendConstraint->getReferencedTableName(); + $localRelatedModel = $models[$keyMap[$keyMapIdLocal]]; $remoteRelatedModel = $models[$keyMap[$keyMapIdRemote]]; // \Kint::dump(array_keys($models), $zendConstraint, $relatedModel);exit; @@ -122,7 +124,7 @@ class Model extends Entity { if ($this->getClassPrefix()) { return - $this->getClassPrefix(). + $this->getClassPrefix() . $this->transStudly2Studly->transform($this->getTableSanitised()); } @@ -217,7 +219,7 @@ class Model extends Entity return $this->columns[$name]; } - throw new BenzineException("Cannot find a Column called {$name} in ".implode(', ', array_keys($this->getColumns()))); + throw new BenzineException("Cannot find a Column called {$name} in " . implode(', ', array_keys($this->getColumns()))); } public function hasColumn(string $columName): bool @@ -258,8 +260,8 @@ class Model extends Entity foreach ($columns as $column) { /** @var ColumnObject $column */ - $typeFragments = explode(' ', $column->getDataType()); - $dbColumnName = $column->getName(); + $typeFragments = explode(' ', $column->getDataType()); + $dbColumnName = $column->getName(); $codeColumnName = $this->sanitiseColumnName($column->getName()); $oColumn = Column::Factory($this->getLaminator()) @@ -281,7 +283,7 @@ class Model extends Entity case 'Postgresql': if ('USER-DEFINED' == $column->getDataType()) { - $enumName = explode('::', $column->getColumnDefault(), 2)[1]; + $enumName = explode('::', $column->getColumnDefault(), 2)[1]; $permittedValues = []; foreach ($this->getDatabase()->getAdaptor()->query("SELECT unnest(enum_range(NULL::{$enumName})) AS option")->execute() as $aiColumn) { $permittedValues[] = $aiColumn['option']; @@ -343,26 +345,26 @@ class Model extends Entity { return [ 'namespace' => $this->getNamespace(), - 'database' => $this->getDatabase()->getName(), - 'table' => $this->getTable(), - 'app_name' => $this->getLaminator()->getBenzineConfig()->getAppName(), - 'app_core' => $this->getLaminator()->getBenzineConfig()->getCore(), + 'database' => $this->getDatabase()->getName(), + 'table' => $this->getTable(), + 'app_name' => $this->getLaminator()->getBenzineConfig()->getAppName(), + 'app_core' => $this->getLaminator()->getBenzineConfig()->getCore(), // 'app_container' => $this->getLaminator()->getBenzineConfig()->getAppContainer(), - 'class_name' => $this->getClassName(), - 'endpoint_name' => $this->getEndpointName(), - 'variable_name' => $this->transStudly2Camel->transform($this->getClassName()), - 'name' => $this->getClassName(), - 'object_name_plural' => Inflect::pluralize($this->getClassName()), - 'object_name_singular' => $this->getClassName(), - 'controller_route' => $this->transCamel2Snake->transform(Inflect::pluralize($this->getClassName())), - 'namespace_model' => "{$this->getNamespace()}\\Models\\{$this->getClassName()}Model", - 'columns' => $this->columns, - 'related_objects' => $this->getRelatedObjects(), + 'class_name' => $this->getClassName(), + 'endpoint_name' => $this->getEndpointName(), + 'variable_name' => $this->transStudly2Camel->transform($this->getClassName()), + 'name' => $this->getClassName(), + 'object_name_plural' => Inflect::pluralize($this->getClassName()), + 'object_name_singular' => $this->getClassName(), + 'controller_route' => $this->transCamel2Snake->transform(Inflect::pluralize($this->getClassName())), + 'namespace_model' => "{$this->getNamespace()}\\Models\\{$this->getClassName()}Model", + 'columns' => $this->columns, + 'related_objects' => $this->getRelatedObjects(), 'related_objects_shared' => $this->getRelatedObjectsSharedAssets(), - 'remote_objects' => $this->getRemoteObjects(), - 'primary_keys' => $this->getPrimaryKeys(), - 'primary_parameters' => $this->getPrimaryParameters(), - 'autoincrement_keys' => $this->getAutoIncrements(), + 'remote_objects' => $this->getRemoteObjects(), + 'primary_keys' => $this->getPrimaryKeys(), + 'primary_parameters' => $this->getPrimaryParameters(), + 'autoincrement_keys' => $this->getAutoIncrements(), // @todo: work out why there are two. 'autoincrement_parameters' => $this->getAutoIncrements(), ]; @@ -407,6 +409,7 @@ class Model extends Entity foreach ($this->getRelatedObjects() as $relatedObject) { $sharedAssets[$relatedObject->getRemoteClass()] = $relatedObject; } + // if(count($this->getRelatedObjects())) { // \Kint::dump($this->getRelatedObjects(), $sharedAssets); // exit; @@ -514,7 +517,7 @@ class Model extends Entity } } if (Laminator::BenzineConfig()->has("benzine/databases/{$database}/column_options/_/transform")) { - $transform = Laminator::BenzineConfig()->get("benzine/databases/{$database}/column_options/_/transform"); + $transform = Laminator::BenzineConfig()->get("benzine/databases/{$database}/column_options/_/transform"); $columnName = $this->getLaminator()->{$transform}->transform($columnName); } if (Laminator::BenzineConfig()->has("benzine/databases/{$database}/column_options/_/replace")) { diff --git a/src/Components/RelatedModel.php b/src/Components/RelatedModel.php index 6b9617f..1c2cdea 100644 --- a/src/Components/RelatedModel.php +++ b/src/Components/RelatedModel.php @@ -1,5 +1,7 @@ transStudly2Camel->transform( - $this->getRemoteClassPrefix(). + $this->getRemoteClassPrefix() . $this->transCamel2Studly->transform($this->getRemoteTableSanitised()) ); } @@ -102,7 +104,7 @@ class RelatedModel extends Entity public function getLocalVariable(): string { return $this->transStudly2Camel->transform( - $this->getLocalClassPrefix(). + $this->getLocalClassPrefix() . $this->transCamel2Studly->transform($this->getLocalTableSanitised()) ); } @@ -126,7 +128,7 @@ class RelatedModel extends Entity public function getLocalBoundColumnAsConstant(): string { - return 'FIELD_'.str_replace('_', '', $this->transCamel2ScreamingSnake->transform($this->getLocalBoundColumn())); + return 'FIELD_' . str_replace('_', '', $this->transCamel2ScreamingSnake->transform($this->getLocalBoundColumn())); } public function getLocalTable(): string @@ -145,7 +147,7 @@ class RelatedModel extends Entity { return $this->transCamel2Studly->transform( $this->getLocalClass() - .'TableGateway' + . 'TableGateway' ); } @@ -153,7 +155,7 @@ class RelatedModel extends Entity { return $this->transCamel2Studly->transform( $this->getRemoteClass() - .'TableGateway' + . 'TableGateway' ); } @@ -161,7 +163,7 @@ class RelatedModel extends Entity { return $this->transCamel2Studly->transform( $this->getLocalClass() - .'Model' + . 'Model' ); } @@ -169,7 +171,7 @@ class RelatedModel extends Entity { return $this->transCamel2Studly->transform( $this->getRemoteClass() - .'Model' + . 'Model' ); } @@ -177,10 +179,9 @@ class RelatedModel extends Entity { if ($this->hasClassConflict()) { return - self::singulariseCamelCaseSentence($this->getLocalClass()). - 'By'. - $this->transCamel2Studly->transform($this->getLocalBoundColumn()) - ; + self::singulariseCamelCaseSentence($this->getLocalClass()) . + 'By' . + $this->transCamel2Studly->transform($this->getLocalBoundColumn()); } return $this->transCamel2Studly->transform( @@ -192,8 +193,8 @@ class RelatedModel extends Entity { if ($this->hasClassConflict()) { return - self::singulariseCamelCaseSentence($this->getRemoteClass()). - 'By'. + self::singulariseCamelCaseSentence($this->getRemoteClass()) . + 'By' . $this->transCamel2Studly->transform($this->getLocalBoundColumn()); } @@ -210,7 +211,7 @@ class RelatedModel extends Entity public function getLocalClass(): string { - return $this->getLocalClassPrefix(). + return $this->getLocalClassPrefix() . $this->transCamel2Studly->transform($this->getLocalTableSanitised()); } @@ -233,28 +234,28 @@ class RelatedModel extends Entity public function getRemoteClass(): string { - return $this->getRemoteClassPrefix(). + return $this->getRemoteClassPrefix() . $this->transCamel2Studly->transform($this->getRemoteTableSanitised()); } public function getLocalBoundColumnGetter(): string { - return 'get'.$this->transCamel2Studly->transform($this->getLocalBoundColumn()); + return 'get' . $this->transCamel2Studly->transform($this->getLocalBoundColumn()); } public function getRemoteBoundColumnGetter(): string { - return 'get'.$this->transCamel2Studly->transform($this->getRemoteBoundColumn()); + return 'get' . $this->transCamel2Studly->transform($this->getRemoteBoundColumn()); } public function getLocalBoundColumnSetter(): string { - return 'set'.$this->transCamel2Studly->transform($this->getLocalBoundColumn()); + return 'set' . $this->transCamel2Studly->transform($this->getLocalBoundColumn()); } public function getRemoteBoundColumnSetter(): string { - return 'set'.$this->transCamel2Studly->transform($this->getRemoteBoundColumn()); + return 'set' . $this->transCamel2Studly->transform($this->getRemoteBoundColumn()); } public function getRemoteBoundColumn(): string @@ -269,7 +270,7 @@ class RelatedModel extends Entity public function getRemoteBoundColumnAsConstant(): string { - return 'FIELD_'.str_replace('_', '', $this->transCamel2ScreamingSnake->transform($this->getRemoteBoundColumn())); + return 'FIELD_' . str_replace('_', '', $this->transCamel2ScreamingSnake->transform($this->getRemoteBoundColumn())); } public function setRemoteBoundColumn(string $remoteBoundColumn): RelatedModel @@ -345,7 +346,7 @@ class RelatedModel extends Entity */ private function singulariseCamelCaseSentence(string $camel): string { - $snake = explode('_', $this->transCamel2Snake->transform($camel)); + $snake = explode('_', $this->transCamel2Snake->transform($camel)); $snake[count($snake) - 1] = Inflect::singularize($snake[count($snake) - 1]); return $this->transSnake2Camel->transform(implode('_', $snake)); diff --git a/src/Connection/Database.php b/src/Connection/Database.php index 94995f1..5a39a63 100644 --- a/src/Connection/Database.php +++ b/src/Connection/Database.php @@ -1,5 +1,7 @@ setName($name); @@ -163,19 +165,11 @@ class Database return new Metadata($this->getAdapter()); } - /** - * @return array - */ public function getIgnoredTables(): array { return $this->ignoredTables; } - /** - * @param array $ignoredTables - * - * @return Database - */ public function setIgnoredTables(array $ignoredTables): Database { $this->ignoredTables = $ignoredTables; @@ -186,14 +180,14 @@ class Database public function getArray(): array { return [ - 'driver' => 'pdo', + 'driver' => 'pdo', 'pdodriver' => $this->getType(), - 'type' => $this->getType(), - 'charset' => $this->getCharset(), - 'host' => $this->getHostname(), - 'username' => $this->getUsername(), - 'password' => $this->getPassword(), - 'database' => $this->getDatabase(), + 'type' => $this->getType(), + 'charset' => $this->getCharset(), + 'host' => $this->getHostname(), + 'username' => $this->getUsername(), + 'password' => $this->getPassword(), + 'database' => $this->getDatabase(), ]; } diff --git a/src/Connection/Databases.php b/src/Connection/Databases.php index 29ac8a7..d2aa734 100644 --- a/src/Connection/Databases.php +++ b/src/Connection/Databases.php @@ -1,5 +1,7 @@ configurationService = $configurationService; - $this->logger = $logger; + $this->logger = $logger; foreach ($this->configurationService->get('databases') as $name => $config) { if (!isset(self::$databases[$name])) { diff --git a/src/Entities/AbstractEntity.php b/src/Entities/AbstractEntity.php index 1275a76..f6f3fd4 100644 --- a/src/Entities/AbstractEntity.php +++ b/src/Entities/AbstractEntity.php @@ -1,7 +1,7 @@ [], + private array $config = [ + 'templates' => [], 'formatting' => [], - 'sql' => [], - 'clean' => [], + 'sql' => [], + 'clean' => [], ]; private static bool $useClassPrefixes = false; private TwigFileSystemLoader $loader; @@ -49,20 +55,20 @@ class Laminator private bool $waitForKeypressEnabled = true; private array $defaultEnvironment = []; - private array $defaultHeaders = []; + private array $defaultHeaders = []; private int $expectedFileOwner; private int $expectedFileGroup; private int $expectedPermissions; public function __construct(string $workPath, ConfigurationService $benzineConfig, Databases $databases) { - $this->workPath = $workPath; + $this->workPath = $workPath; self::$benzineConfig = $benzineConfig; - $this->databases = $databases; + $this->databases = $databases; - $script = realpath($_SERVER['SCRIPT_FILENAME']); - $this->expectedFileOwner = fileowner($script); - $this->expectedFileGroup = filegroup($script); + $script = realpath($_SERVER['SCRIPT_FILENAME']); + $this->expectedFileOwner = fileowner($script); + $this->expectedFileGroup = filegroup($script); $this->expectedPermissions = fileperms($script); set_exception_handler([$this, 'exceptionHandler']); @@ -70,7 +76,7 @@ class Laminator $this->defaultEnvironment = [ 'SCRIPT_NAME' => '/index.php', - 'RAND' => rand(0, 100000000), + 'RAND' => random_int(0, 100000000), ]; $this->defaultHeaders = []; } @@ -88,9 +94,9 @@ class Laminator $this->setWorkPath(self::$benzineConfig->get(ConfigurationService::KEY_APP_ROOT)); } - $this->loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/Generator/Templates'); - $this->twig = new \Twig\Environment($this->loader, ['debug' => true]); - $this->twig->addExtension(new \Twig\Extension\DebugExtension()); + $this->loader = new FilesystemLoader(__DIR__ . '/Generator/Templates'); + $this->twig = new Environment($this->loader, ['debug' => true]); + $this->twig->addExtension(new DebugExtension()); $this->twig->addExtension(new TransformExtension()); $this->twig->addExtension(new InflectionExtension()); @@ -98,16 +104,16 @@ class Laminator new ArrayUniqueTwigExtension() ); - $fct = new \Twig\TwigFunction('var_export', 'var_export'); + $fct = new TwigFunction('var_export', 'var_export'); $this->twig->addFunction($fct); - $this->transSnake2Studly = new CaseTransformer(new Format\SnakeCase(), new Format\StudlyCaps()); - $this->transStudly2Camel = new CaseTransformer(new Format\StudlyCaps(), new Format\CamelCase()); + $this->transSnake2Studly = new CaseTransformer(new Format\SnakeCase(), new Format\StudlyCaps()); + $this->transStudly2Camel = new CaseTransformer(new Format\StudlyCaps(), new Format\CamelCase()); $this->transStudly2Studly = new CaseTransformer(new Format\StudlyCaps(), new Format\StudlyCaps()); - $this->transCamel2Studly = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); - $this->transSnake2Camel = new CaseTransformer(new Format\SnakeCase(), new Format\CamelCase()); - $this->transSnake2Spinal = new CaseTransformer(new Format\SnakeCase(), new Format\SpinalCase()); - $this->transCamel2Snake = new CaseTransformer(new Format\CamelCase(), new Format\SnakeCase()); + $this->transCamel2Studly = new CaseTransformer(new Format\CamelCase(), new Format\StudlyCaps()); + $this->transSnake2Camel = new CaseTransformer(new Format\SnakeCase(), new Format\CamelCase()); + $this->transSnake2Spinal = new CaseTransformer(new Format\SnakeCase(), new Format\SpinalCase()); + $this->transCamel2Snake = new CaseTransformer(new Format\CamelCase(), new Format\SnakeCase()); return $this; } @@ -138,12 +144,12 @@ class Laminator { // UHOH exception handler /** @var \Exception $exception */ - echo "\n".ConsoleHelper::COLOR_RED; + echo "\n" . ConsoleHelper::COLOR_RED; echo " ____ ____ ____ ____ \n"; echo "||U |||H |||O |||H ||\n"; echo "||__|||__|||__|||__||\n"; echo "|/__\\|/__\\|/__\\|/__\\|\n"; - echo ConsoleHelper::COLOR_RESET."\n\n"; + echo ConsoleHelper::COLOR_RESET . "\n\n"; echo $exception->getMessage(); echo "\n\n"; echo "In {$exception->getFile()}:{$exception->getLine()}"; @@ -171,11 +177,9 @@ class Laminator } /** - * @param $schemaName + * @return int|string * * @throws SchemaToAdaptorException - * - * @return int|string */ public function schemaName2databaseName($schemaName) { @@ -228,8 +232,8 @@ class Laminator { switch ($database->getAdapter()->getDriver()->getDatabasePlatformName()) { case 'Mysql': - $sql = "SHOW columns FROM `{$table}` WHERE extra LIKE '%auto_increment%'"; - $query = $database->getAdapter()->query($sql); + $sql = "SHOW columns FROM `{$table}` WHERE extra LIKE '%auto_increment%'"; + $query = $database->getAdapter()->query($sql); $columns = []; foreach ($query->execute() as $aiColumn) { @@ -239,8 +243,8 @@ class Laminator return $columns; case 'Postgresql': - $sql = "SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_NAME = '{$table}' AND column_default LIKE 'nextval(%'"; - $query = $database->getAdapter()->query($sql); + $sql = "SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_NAME = '{$table}' AND column_default LIKE 'nextval(%'"; + $query = $database->getAdapter()->query($sql); $columns = []; foreach ($query->execute() as $aiColumn) { @@ -255,11 +259,11 @@ class Laminator } /** + * @return $this + * * @throws LoaderError * @throws RuntimeError * @throws SyntaxError - * - * @return $this */ public function makeLaminator() { @@ -280,7 +284,7 @@ class Laminator { /** @var Model[] $models */ $models = []; - $keys = []; + $keys = []; foreach ($this->databases->getAll() as $dbName => $database) { /** @var Database $database */ echo "Database: {$dbName}\n"; @@ -288,13 +292,13 @@ class Laminator /** @var TableObject $tables */ $tables = $database->getMetadata()->getTables(); - echo 'Collecting '.count($tables)." entities data.\n"; + echo 'Collecting ' . count($tables) . " entities data.\n"; foreach ($tables as $table) { if (in_array($table->getName(), $database->getIgnoredTables(), true)) { continue; } - $oModel = Components\Model::Factory($this) + $oModel = Model::Factory($this) ->setClassPrefix(self::$benzineConfig->get("databases/{$dbName}/class_prefix", null)) ->setNamespace(self::$benzineConfig->getNamespace()) ->setDatabase($database) @@ -304,8 +308,8 @@ class Laminator if (self::$benzineConfig->has("databases/{$dbName}/class_prefix")) { $oModel->setClassPrefix(self::$benzineConfig->get("databases/{$dbName}/class_prefix")); } - $models[$oModel->getClassName()] = $oModel; - $keys[$database->getAdapter()->getCurrentSchema().'::'.$table->getName()] = $oModel->getClassName(); + $models[$oModel->getClassName()] = $oModel; + $keys[$database->getAdapter()->getCurrentSchema() . '::' . $table->getName()] = $oModel->getClassName(); } } ksort($models); @@ -317,7 +321,7 @@ class Laminator if (in_array($table->getName(), $database->getIgnoredTables(), true)) { continue; } - $key = $keys[$database->getAdapter()->getCurrentSchema().'::'.$table->getName()]; + $key = $keys[$database->getAdapter()->getCurrentSchema() . '::' . $table->getName()]; $models[$key] ->computeColumns($table->getColumns()) ->computeConstraints($models, $keys, $table->getConstraints()) @@ -391,15 +395,15 @@ class Laminator /** * @param Model[] $models * + * @return Laminator + * * @throws LoaderError When the template cannot be found * @throws SyntaxError When an error occurred during compilation * @throws RuntimeError When an error occurred during rendering - * - * @return Laminator */ private function makeCoreFiles(array $models) { - echo 'Generating Core files for '.count($models)." models... \n"; + echo 'Generating Core files for ' . count($models) . " models... \n"; $allModelData = []; foreach ($models as $model) { $allModelData[$model->getClassName()] = $model->getRenderDataset(); @@ -447,10 +451,10 @@ class Laminator private function renderToFile(bool $overwrite, string $path, string $template, array $data) { $output = $this->twig->render($template, $data); - $path = $this->getWorkPath().'/'.$path; + $path = $this->getWorkPath() . '/' . $path; if (!(new Filesystem())->exists(dirname($path))) { - (new Filesystem())->mkdir(dirname($path), 0777); + (new Filesystem())->mkdir(dirname($path), 0o777); } if (!(new Filesystem())->exists($path) || $overwrite) { // printf(" [Done]" . PHP_EOL); diff --git a/src/LaminatorSql.php b/src/LaminatorSql.php index 2da22aa..795f755 100644 --- a/src/LaminatorSql.php +++ b/src/LaminatorSql.php @@ -1,10 +1,13 @@ ['Yes', 'No'], + 'values' => ['Yes', 'No'], 'default' => 'No', ]; } diff --git a/src/Migrations/AbstractSeed.php b/src/Migrations/AbstractSeed.php index 9d5aab1..c2d2bfd 100644 --- a/src/Migrations/AbstractSeed.php +++ b/src/Migrations/AbstractSeed.php @@ -1,5 +1,7 @@ faker = App::Instance()->get(Generator::class); - $this->log = App::Instance()->get(Logger::class); + $this->log = App::Instance()->get(Logger::class); } } diff --git a/src/Profiler/Profiler.php b/src/Profiler/Profiler.php index 4e33907..97e788d 100644 --- a/src/Profiler/Profiler.php +++ b/src/Profiler/Profiler.php @@ -1,5 +1,7 @@ count($this->queryTimes), - 'TotalTime' => array_sum($this->queryTimes), - 'Diagnostic' => $this->getQueries($queryStatisticClass), + 'TotalTime' => array_sum($this->queryTimes), + 'Diagnostic' => $this->getQueries($queryStatisticClass), ]; } @@ -47,25 +47,25 @@ class Profiler implements ProfilerInterface public function profilerFinish(): void { - $uuid = UUID::v4(); + $uuid = UUID::v4(); $executionTime = microtime(true) - $this->timer; // $this->logger->addDebug("Query \"{$this->sql}\" took {$executionTime} sec"); $this->queryTimes[$uuid] = $executionTime; - $this->queries[$uuid] = [$this->sql, debug_backtrace()]; - $this->sql = null; - $this->timer = null; + $this->queries[$uuid] = [$this->sql, debug_backtrace()]; + $this->sql = null; + $this->timer = null; } /** * @return QueryStatisticInterface[] */ - public function getQueries(QueryStatisticInterface $queryStatisticClass = null): array + public function getQueries(?QueryStatisticInterface $queryStatisticClass = null): array { $stats = []; foreach ($this->queries as $uuid => [$query, $backTrace]) { if ($queryStatisticClass) { if (is_object($queryStatisticClass)) { - $queryStatisticClass = get_class($queryStatisticClass); + $queryStatisticClass = $queryStatisticClass::class; } $stat = new $queryStatisticClass(); } else { diff --git a/src/Profiler/QueryStatistic.php b/src/Profiler/QueryStatistic.php index b133bd1..9aa7c72 100644 --- a/src/Profiler/QueryStatistic.php +++ b/src/Profiler/QueryStatistic.php @@ -1,5 +1,7 @@ number_format($this->getTime() * 1000, 3).'ms', + 'Time' => number_format($this->getTime() * 1000, 3) . 'ms', 'Query' => $this->getSql(), ]; } diff --git a/src/TabularData/Table.php b/src/TabularData/Table.php index 668c0e6..fd78150 100644 --- a/src/TabularData/Table.php +++ b/src/TabularData/Table.php @@ -1,5 +1,7 @@ service = $service; - $this->setName(get_class($service)); + $this->setName($service::class); $this->reload(); } diff --git a/src/TabularData/TableRow.php b/src/TabularData/TableRow.php index d3cae69..6a90dfd 100644 --- a/src/TabularData/TableRow.php +++ b/src/TabularData/TableRow.php @@ -1,5 +1,7 @@ getByField($field, $this->data[$field]); + $relatedEntity = $service->getByField($field, $this->data[$field]); $this->related[$field] = $relatedEntity; } } diff --git a/src/Transaction.php b/src/Transaction.php index 45d5c77..a8a9443 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -1,5 +1,7 @@