diff --git a/application/Espo/Core/Utils/Database/Helper.php b/application/Espo/Core/Utils/Database/Helper.php index 1cb8c0f3f1..bc9ff7e0d4 100644 --- a/application/Espo/Core/Utils/Database/Helper.php +++ b/application/Espo/Core/Utils/Database/Helper.php @@ -30,7 +30,6 @@ namespace Espo\Core\Utils\Database; use Espo\Core\{ - Exceptions\Error, Utils\Config, }; @@ -51,11 +50,11 @@ use RuntimeException; class Helper { - private $config; + private ?Config $config; - private $dbalConnection; + private ?DbalConnection $dbalConnection = null; - private $pdoConnection; + private ?PDO $pdoConnection = null; private $driverPlatformMap = [ 'pdo_mysql' => 'Mysql', @@ -79,7 +78,7 @@ class Helper $this->config = $config; } - public function getDbalConnection() + public function getDbalConnection(): DbalConnection { if (!isset($this->dbalConnection)) { $this->dbalConnection = $this->createDbalConnection(); @@ -88,7 +87,7 @@ class Helper return $this->dbalConnection; } - public function getPdoConnection() + public function getPdoConnection(): PDO { if (!isset($this->pdoConnection)) { $this->pdoConnection = $this->createPdoConnection(); @@ -97,17 +96,17 @@ class Helper return $this->pdoConnection; } - public function setDbalConnection(DbalConnection $dbalConnection) + public function setDbalConnection(DbalConnection $dbalConnection): void { $this->dbalConnection = $dbalConnection; } - public function setPdoConnection(PDO $pdoConnection) + public function setPdoConnection(PDO $pdoConnection): void { $this->pdoConnection = $pdoConnection; } - public function createDbalConnection(array $params = []) + public function createDbalConnection(array $params = []): DbalConnection { if (empty($params) && isset($this->config)) { $params = $this->config->get('database'); diff --git a/application/Espo/Core/Utils/Database/Schema/Schema.php b/application/Espo/Core/Utils/Database/Schema/Schema.php index 0f21133835..bd03794959 100644 --- a/application/Espo/Core/Utils/Database/Schema/Schema.php +++ b/application/Espo/Core/Utils/Database/Schema/Schema.php @@ -33,6 +33,8 @@ use Doctrine\DBAL\{ Types\Type, Schema\SchemaDiff as DBALSchemaDiff, Schema\Schema as DBALSchema, + Connection, + Platforms\AbstractPlatform, }; use Espo\Core\{ @@ -54,38 +56,37 @@ use Throwable; class Schema { - private $config; + private Config $config; - private $metadata; + private Metadata $metadata; - private $fileManager; + private FileManager $fileManager; - private $entityManager; + private EntityManager $entityManager; - private $classMap; + private ClassMap $classMap; - private $comparator; + private Comparator $comparator; - private $converter; + private DatabaseConverter $converter; - private $databaseHelper; + private Helper $databaseHelper; - protected $ormMetadataData; + protected OrmMetadataData $ormMetadataData; - private $log; + private Log $log; - private $fieldTypePath = 'application/Espo/Core/Utils/Database/DBAL/FieldTypes'; + private string $fieldTypePath = 'application/Espo/Core/Utils/Database/DBAL/FieldTypes'; - private $rebuildActionsPath = 'Core/Utils/Database/Schema/rebuildActions'; + private string $rebuildActionsPath = 'Core/Utils/Database/Schema/rebuildActions'; - private $schemaConverter; + private Converter $schemaConverter; /** - * Array of rebuildActions classes in format: - * [ - * 'beforeRebuild' => [...], - * 'afterRebuild' => [...], - * ] + * @var ?array{ + * beforeRebuild: \Espo\Core\Utils\Database\Schema\BaseRebuildActions[], + * afterRebuild: \Espo\Core\Utils\Database\Schema\BaseRebuildActions[], + * } */ protected $rebuildActionClasses = null; @@ -107,7 +108,6 @@ class Schema $this->log = $log; $this->databaseHelper = new Helper($this->config); - $this->comparator = new Comparator(); $this->initFieldTypes(); @@ -126,54 +126,24 @@ class Schema $this->ormMetadataData = $ormMetadataData; } - protected function getConfig() - { - return $this->config; - } - - protected function getMetadata() - { - return $this->metadata; - } - - protected function getFileManager() - { - return $this->fileManager; - } - - protected function getEntityManager() - { - return $this->entityManager; - } - - protected function getComparator() - { - return $this->comparator; - } - - protected function getConverter() - { - return $this->converter; - } - - public function getPlatform() - { - return $this->getConnection()->getDatabasePlatform(); - } - - public function getDatabaseHelper() + public function getDatabaseHelper(): Helper { return $this->databaseHelper; } - public function getConnection() + public function getPlatform(): AbstractPlatform + { + return $this->getConnection()->getDatabasePlatform(); + } + + public function getConnection(): Connection { return $this->getDatabaseHelper()->getDbalConnection(); } - protected function initFieldTypes() + protected function initFieldTypes(): void { - $typeList = $this->getFileManager()->getFileList($this->fieldTypePath, false, '\.php$'); + $typeList = $this->fileManager->getFileList($this->fieldTypePath, false, '\.php$'); foreach ($typeList as $name) { $typeName = preg_replace('/Type\.php$/i', '', $name); @@ -197,12 +167,14 @@ class Schema } } - /* + /** * Rebuild database schema. + * + * @param ?string[] $entityList */ - public function rebuild(?array $entityList = null) : bool + public function rebuild(?array $entityList = null): bool { - if (!$this->getConverter()->process()) { + if (!$this->converter->process()) { return false; } @@ -251,34 +223,34 @@ class Schema return (bool) $result; } - /* - * Get current database schema. - * - * @return \Doctrine\DBAL\Schema\Schema - */ - protected function getCurrentSchema() + /** + * Get current database schema. + */ + protected function getCurrentSchema(): DBALSchema { - return $this->getConnection()->getSchemaManager()->createSchema(); + return $this->getConnection() + ->getSchemaManager() + ->createSchema(); } - /* - * Get SQL queries of database schema. - * - * @return array - array of SQL queries - */ + /** + * Get SQL queries of database schema. + * + * @return string[] Array of SQL queries. + */ public function toSql(DBALSchemaDiff $schema) { return $schema->toSaveSql($this->getPlatform()); } - /* - * Get SQL queries to get from one to another schema. - * - * @return array - array of SQL queries. - */ + /** + * Get SQL queries to get from one to another schema. + * + * @return string[] Array of SQL queries. + */ public function getDiffSql(DBALSchema $fromSchema, DBALSchema $toSchema) { - $schemaDiff = $this->getComparator()->compare($fromSchema, $toSchema); + $schemaDiff = $this->comparator->compare($fromSchema, $toSchema); return $this->toSql($schemaDiff); } @@ -286,13 +258,14 @@ class Schema /** * Init Rebuild Actions, get all classes and create them. */ - protected function initRebuildActions($currentSchema = null, $metadataSchema = null) + protected function initRebuildActions(?DBALSchema $currentSchema = null, ?DBALSchema $metadataSchema = null): void { $methods = [ 'beforeRebuild', 'afterRebuild', ]; + /** @var array> */ $rebuildActions = $this->classMap->getData($this->rebuildActionsPath, null, $methods); $classes = []; @@ -326,18 +299,16 @@ class Schema /** * Execute actions for RebuildAction classes. * - * @param string $action action name, possible values 'beforeRebuild' | 'afterRebuild'. + * @param string $action An action name, 'beforeRebuild' or 'afterRebuild'. */ - protected function executeRebuildActions($action = 'beforeRebuild') + protected function executeRebuildActions(string $action = 'beforeRebuild'): void { if (!isset($this->rebuildActionClasses)) { $this->initRebuildActions(); } - if (isset($this->rebuildActionClasses[$action])) { - foreach ($this->rebuildActionClasses[$action] as $rebuildActionClass) { - $rebuildActionClass->$action(); - } + foreach ($this->rebuildActionClasses[$action] as $rebuildActionClass) { + $rebuildActionClass->$action(); } } } diff --git a/application/Espo/Core/Utils/Database/Schema/SchemaProxy.php b/application/Espo/Core/Utils/Database/Schema/SchemaProxy.php index 1f4b68b47c..fe4fc02fe5 100644 --- a/application/Espo/Core/Utils/Database/Schema/SchemaProxy.php +++ b/application/Espo/Core/Utils/Database/Schema/SchemaProxy.php @@ -36,7 +36,7 @@ use Espo\Core\{ class SchemaProxy { - private $container; + private Container $container; public function __construct(Container $container) { @@ -48,12 +48,15 @@ class SchemaProxy return $this->container->get('schema'); } - public function rebuild(?array $entityList = null) : bool + /** + * @param ?string[] $entityList + */ + public function rebuild(?array $entityList = null): bool { return $this->getSchema()->rebuild(); } - public function getDatabaseHelper() : Helper + public function getDatabaseHelper(): Helper { return $this->getSchema()->getDatabaseHelper(); } diff --git a/application/Espo/Core/Utils/Database/Schema/Utils.php b/application/Espo/Core/Utils/Database/Schema/Utils.php index 6871b97192..0b51b487ea 100644 --- a/application/Espo/Core/Utils/Database/Schema/Utils.php +++ b/application/Espo/Core/Utils/Database/Schema/Utils.php @@ -33,6 +33,11 @@ use Espo\Core\Utils\Util; class Utils { + /** + * @param array $ormMeta + * @param string[] $ignoreFlags + * @return array> + */ public static function getIndexes(array $ormMeta, array $ignoreFlags = []) { $indexList = []; @@ -87,6 +92,10 @@ class Utils return $indexList; } + /** + * @param array $fieldDefs + * @return ?string + */ public static function getIndexTypeByFieldDefs(array $fieldDefs) { if ($fieldDefs['type'] != 'id' && isset($fieldDefs['unique']) && $fieldDefs['unique']) { @@ -96,8 +105,16 @@ class Utils if (isset($fieldDefs['index']) && $fieldDefs['index']) { return 'index'; } + + return null; } + /** + * @param string $fieldName + * @param array $fieldDefs + * @param mixed $default + * @return mixed + */ public static function getIndexNameByFieldDefs($fieldName, array $fieldDefs, $default = null) { $indexType = static::getIndexTypeByFieldDefs($fieldDefs); @@ -117,6 +134,11 @@ class Utils return $default; } + /** + * @param array $fieldsDefs + * @param bool $isTableColumnNames + * @return array + */ public static function getEntityIndexListByFieldsDefs(array $fieldsDefs, $isTableColumnNames = false) { $indexList = []; @@ -150,6 +172,10 @@ class Utils return $indexList; } + /** + * @param array $indexDefs + * @return string + */ public static function getIndexTypeByIndexDefs(array $indexDefs) { if ( @@ -166,6 +192,12 @@ class Utils return 'index'; } + /** + * @param string $name + * @param string $type + * @param int $maxLength + * @return string + */ public static function generateIndexName($name, $type = 'index', $maxLength = 60) { switch ($type) { @@ -185,6 +217,14 @@ class Utils return substr(implode('_', $nameList), 0, $maxLength); } + /** + * + * @param array $ormMeta + * @param int $indexMaxLength + * @param ?array $indexList + * @param int $characterLength + * @return array + */ public static function getFieldListExceededIndexMaxLength( array $ormMeta, $indexMaxLength = 1000, @@ -244,6 +284,11 @@ class Utils return $fields; } + /** + * @param array $ormFieldDefs + * @param int $characterLength + * @return int + */ protected static function getFieldLength(array $ormFieldDefs, $characterLength = 4) { $length = 0; @@ -264,7 +309,7 @@ class Utils $type = static::getDbFieldType($ormFieldDefs); $length = isset($defaultLength[$type]) ? $defaultLength[$type] : $length; - $length = isset($ormFieldDefs['len']) ? $ormFieldDefs['len'] : $length; + //$length = isset($ormFieldDefs['len']) ? $ormFieldDefs['len'] : $length; switch ($type) { case 'varchar': @@ -275,11 +320,19 @@ class Utils return $length; } + /** + * @param array $ormFieldDefs + * @return string + */ protected static function getDbFieldType(array $ormFieldDefs) { return isset($ormFieldDefs['dbType']) ? $ormFieldDefs['dbType'] : $ormFieldDefs['type']; } + /** + * @param array $ormFieldDefs + * @return string + */ protected static function getFieldType(array $ormFieldDefs) { return isset($ormFieldDefs['type']) ? $ormFieldDefs['type'] : static::getDbFieldType($ormFieldDefs); diff --git a/application/Espo/Core/Utils/Database/Schema/rebuildActions/AddSystemUser.php b/application/Espo/Core/Utils/Database/Schema/rebuildActions/AddSystemUser.php index 60068deb4a..f06d5e9a39 100644 --- a/application/Espo/Core/Utils/Database/Schema/rebuildActions/AddSystemUser.php +++ b/application/Espo/Core/Utils/Database/Schema/rebuildActions/AddSystemUser.php @@ -33,6 +33,9 @@ use Espo\Core\Utils\Database\Schema\BaseRebuildActions as Base; class AddSystemUser extends Base { + /** + * @return void + */ public function afterRebuild() { $userId = $this->getConfig()->get('systemUserAttributes.id'); diff --git a/application/Espo/Core/Utils/Database/Schema/rebuildActions/Currency.php b/application/Espo/Core/Utils/Database/Schema/rebuildActions/Currency.php index 82d3e1528b..11bfd65565 100644 --- a/application/Espo/Core/Utils/Database/Schema/rebuildActions/Currency.php +++ b/application/Espo/Core/Utils/Database/Schema/rebuildActions/Currency.php @@ -33,6 +33,9 @@ use Espo\Core\Utils\Currency\DatabasePopulator; class Currency extends \Espo\Core\Utils\Database\Schema\BaseRebuildActions { + /** + * @return void + */ public function afterRebuild() { $populator = new DatabasePopulator($this->getConfig(), $this->getEntityManager()); diff --git a/application/Espo/Core/Utils/Database/Schema/rebuildActions/FulltextIndex.php b/application/Espo/Core/Utils/Database/Schema/rebuildActions/FulltextIndex.php index 5f4847339e..8679b9ba47 100644 --- a/application/Espo/Core/Utils/Database/Schema/rebuildActions/FulltextIndex.php +++ b/application/Espo/Core/Utils/Database/Schema/rebuildActions/FulltextIndex.php @@ -36,6 +36,9 @@ use Exception; class FulltextIndex extends BaseRebuildActions { + /** + * @return void + */ public function beforeRebuild() { $currentSchema = $this->getCurrentSchema(); diff --git a/application/Espo/Core/Utils/File/ClassMap.php b/application/Espo/Core/Utils/File/ClassMap.php index 10ef0f6bc3..8a490be3d0 100644 --- a/application/Espo/Core/Utils/File/ClassMap.php +++ b/application/Espo/Core/Utils/File/ClassMap.php @@ -75,7 +75,7 @@ class ClassMap * Return paths to class files. * * @param ?string[] $allowedMethods If specified, classes w/o specified method will be ignored. - * @return array + * @return array */ public function getData( string $path, @@ -138,7 +138,7 @@ class ClassMap /** * @param string[]|string $dirs * @param ?string[] $allowedMethods - * @return array + * @return array */ private function getClassNameHash($dirs, ?array $allowedMethods = [], bool $subDirs = false): array { @@ -162,7 +162,7 @@ class ClassMap /** * @param string[] $fileList * @param ?string[] $allowedMethods - * @param array $data + * @param array $data */ private function fillHashFromFileList( array $fileList,