diff --git a/composer.lock b/composer.lock index b735b0e..a97af1e 100644 --- a/composer.lock +++ b/composer.lock @@ -4008,16 +4008,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.24", + "version": "9.5.25", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "d0aa6097bef9fd42458a9b3c49da32c6ce6129c5" + "reference": "3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/d0aa6097bef9fd42458a9b3c49da32c6ce6129c5", - "reference": "d0aa6097bef9fd42458a9b3c49da32c6ce6129c5", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d", + "reference": "3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d", "shasum": "" }, "require": { @@ -4039,14 +4039,14 @@ "phpunit/php-timer": "^5.0.2", "sebastian/cli-parser": "^1.0.1", "sebastian/code-unit": "^1.0.6", - "sebastian/comparator": "^4.0.5", + "sebastian/comparator": "^4.0.8", "sebastian/diff": "^4.0.3", "sebastian/environment": "^5.1.3", - "sebastian/exporter": "^4.0.3", + "sebastian/exporter": "^4.0.5", "sebastian/global-state": "^5.0.1", "sebastian/object-enumerator": "^4.0.3", "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^3.1", + "sebastian/type": "^3.2", "sebastian/version": "^3.0.2" }, "suggest": { @@ -4090,7 +4090,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.24" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.25" }, "funding": [ { @@ -4100,9 +4100,13 @@ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", + "type": "tidelift" } ], - "time": "2022-08-30T07:42:16+00:00" + "time": "2022-09-25T03:44:45+00:00" }, { "name": "psr/container", diff --git a/src/Console/OptionParserTrait.php b/src/Console/OptionParserTrait.php deleted file mode 100644 index dba8fe0..0000000 --- a/src/Console/OptionParserTrait.php +++ /dev/null @@ -1,118 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Migrations\Console; - -use InvalidArgumentException; -use LogicException; -use Phalcon\Migrations\Mvc\Model\Migration as ModelMigration; -use Phalcon\Migrations\Version\IncrementalItem as IncrementalVersion; -use Phalcon\Migrations\Version\ItemCollection as VersionCollection; -use Phalcon\Migrations\Version\ItemInterface; - -use function is_array; -use function microtime; -use function pow; -use function rmdir; -use function rtrim; -use function substr; - -use const DIRECTORY_SEPARATOR; - -/** - * Parsing CLI options - * - * @property array $options - */ -trait OptionParserTrait -{ - /** - * Get prefix from the option - * - * @param string $prefix - * @param mixed $prefixEnd - * - * @return mixed - */ - public function getPrefixOption(string $prefix, $prefixEnd = '*') - { - if (substr($prefix, -1) != $prefixEnd) { - return ''; - } - - return substr($prefix, 0, -1); - } - - /** - * Get version name to generate migration - */ - public function getVersionNameGeneratingMigration(): ItemInterface - { - if (empty($this->options)) { - throw new InvalidArgumentException('Options were not defined yet'); - } - - // Use timestamped version if description is provided - if ($this->options['descr']) { - $this->options['version'] = (string) (int) (microtime(true) * pow(10, 6)); - VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED); - $versionItem = VersionCollection::createItem($this->options['version'] . '_' . $this->options['descr']); - - // Elsewhere use old-style incremental versioning - // The version is specified - } elseif ($this->options['version']) { - VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL); - $versionItem = VersionCollection::createItem($this->options['version']); - //check version is exist - $migrationsDirList = $this->options['migrationsDir']; - if (is_array($migrationsDirList)) { - foreach ($migrationsDirList as $migrationsDir) { - $migrationsSubDirList = ModelMigration::scanForVersions($migrationsDir); - - foreach ($migrationsSubDirList as $item) { - if ($item->getVersion() != $versionItem->getVersion()) { - continue; - } - - if (!$this->options['force']) { - throw new LogicException('Version ' . $item->getVersion() . ' already exists'); - } else { - rmdir(rtrim($migrationsDir, '\\/') . DIRECTORY_SEPARATOR . $versionItem->getVersion()); - } - } - } - } - // The version is guessed automatically - } else { - VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL); - $versionItems = []; - $migrationsDirList = $this->options['migrationsDir']; - - if (is_array($migrationsDirList)) { - foreach ($migrationsDirList as $migrationsDir) { - $versionItems = $versionItems + ModelMigration::scanForVersions($migrationsDir); - } - } - - if (!isset($versionItems[0])) { - $versionItem = VersionCollection::createItem('1.0.0'); - } else { - /** @var IncrementalVersion $versionItem */ - $versionItem = VersionCollection::maximum($versionItems); - $versionItem = $versionItem->addMinor(1); - } - } - - return $versionItem; - } -} diff --git a/src/Console/OptionStack.php b/src/Console/OptionStack.php index 8476a91..9a60c2a 100644 --- a/src/Console/OptionStack.php +++ b/src/Console/OptionStack.php @@ -13,115 +13,181 @@ namespace Phalcon\Migrations\Console; -use function array_merge; +use ArrayAccess; +use LogicException; +use Phalcon\Migrations\Mvc\Model\Migration as ModelMigration; +use Phalcon\Migrations\Version\IncrementalItem as IncrementalVersion; +use Phalcon\Migrations\Version\ItemCollection as VersionCollection; +use Phalcon\Migrations\Version\ItemInterface; /** * CLI options */ -class OptionStack +class OptionStack implements ArrayAccess { - use OptionParserTrait; - /** * Parameters received by the script. * * @var array */ - protected $options = []; + protected array $options = []; /** - * Add option to array - * - * @param mixed $key - * @param mixed $option - * @param mixed $defaultValue + * @param array $options */ - public function setOption($key, $option, $defaultValue = ''): void + public function __construct(array $options = []) { - if (!empty($option)) { - $this->options[$key] = $option; - - return; - } - - $this->options[$key] = $defaultValue; + $this->options = $options; } /** - * Set option if value isn't exist + * Get received options * - * @param string $key - * @param mixed $defaultValue + * @return array */ - public function setDefaultOption(string $key, $defaultValue): void + public function getOptions(): array { - if (!isset($this->options[$key])) { - $this->options[$key] = $defaultValue; - } + return $this->options; } /** - * Get received options - * + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset): bool + { + return isset($this->options[$offset]); + } + + /** + * @param mixed $offset * @return mixed */ - public function getOptions() + #[\ReturnTypeWillChange] + public function offsetGet($offset) { - return $this->options; + return $this->options[$offset] ?? ''; } /** - * Set received options - * - * @param array $options + * @param mixed $offset + * @param mixed $value */ - public function setOptions(array $options): void + public function offsetSet($offset, $value): void { - $this->options = array_merge($this->options, $options); + $this->options[$offset] = $value; } /** - * Get option - * - * @param string $key - * - * @return mixed + * @param mixed $offset + * @param mixed|null $default */ - public function getOption($key) + public function offsetSetDefault($offset, $default = null): void { - if (!isset($this->options[$key])) { - return ''; + if (!array_key_exists($offset, $this->options)) { + $this->options[$offset] = $default; } + } - return $this->options[$key]; + /** + * @param mixed $offset + * @param mixed|null $value + * @param mixed|null $default + */ + public function offsetSetOrDefault($offset, $value = null, $default = null): void + { + $this->options[$offset] = $value ?: $default; + } + + /** + * @param mixed $offset + */ + public function offsetUnset($offset): void + { + if (array_key_exists($offset, $this->options)) { + unset($this->options[$offset]); + } } /** - * Get option if existence or get default option + * Get prefix from the option * - * @param string $key - * @param mixed $defaultOption + * @param string $prefix + * @param mixed $prefixEnd * * @return mixed */ - public function getValidOption($key, $defaultOption = '') + public function getPrefixOption(string $prefix, $prefixEnd = '*') { - if (isset($this->options[$key])) { - return $this->options[$key]; + if (substr($prefix, -1) != $prefixEnd) { + return ''; } - return $defaultOption; + return substr($prefix, 0, -1); } /** - * Indicates whether the script was a particular option. - * - * @param string $key + * Get version name to generate migration * - * @return bool + * @return ItemInterface */ - public function isReceivedOption($key): bool + public function getVersionNameGeneratingMigration(): ItemInterface { - return isset($this->options[$key]); + /** + * Use timestamped version if description is provided. + */ + if (isset($this->options['descr'])) { + $this->options['version'] = (string) (int) (microtime(true) * pow(10, 6)); + VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED); + + return VersionCollection::createItem($this->options['version'] . '_' . $this->options['descr']); + } + + VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL); + $migrationsDirList = is_array($this->options['migrationsDir']) ? $this->options['migrationsDir'] : []; + + /** + * Elsewhere, use old-style incremental versioning. + * The version is specified. + */ + if (isset($this->options['version'])) { + $versionItem = VersionCollection::createItem($this->options['version']); + // Check version if exists. + foreach ($migrationsDirList as $migrationsDir) { + $migrationsSubDirList = ModelMigration::scanForVersions($migrationsDir); + + foreach ($migrationsSubDirList as $item) { + if ($item->getVersion() != $versionItem->getVersion()) { + continue; + } + + if (!$this->options['force']) { + throw new LogicException('Version ' . $item->getVersion() . ' already exists'); + } else { + rmdir(rtrim($migrationsDir, '\\/') . DIRECTORY_SEPARATOR . $versionItem->getVersion()); + } + } + } + + return $versionItem; + } + + /** + * The version is guessed automatically + */ + $versionItems = []; + foreach ($migrationsDirList as $migrationsDir) { + $versionItems = $versionItems + ModelMigration::scanForVersions($migrationsDir); + } + + if (!isset($versionItems[0])) { + $versionItem = VersionCollection::createItem('1.0.0'); + } else { + /** @var IncrementalVersion $versionItem */ + $versionItem = VersionCollection::maximum($versionItems); + $versionItem = $versionItem->addMinor(1); + } + + return $versionItem; } } diff --git a/src/Db/FieldDefinition.php b/src/Db/FieldDefinition.php index 19adf92..91459a1 100644 --- a/src/Db/FieldDefinition.php +++ b/src/Db/FieldDefinition.php @@ -22,22 +22,22 @@ class FieldDefinition /** * @var string */ - private $name; + private string $name; /** * @var ColumnInterface */ - private $currentColumn; + private ColumnInterface $currentColumn; /** * @var FieldDefinition|null */ - private $previousField; + private ?FieldDefinition $previousField; /** * @var FieldDefinition|null */ - private $nextField; + private ?FieldDefinition $nextField; public function __construct( ColumnInterface $column, @@ -98,6 +98,7 @@ public function getPairedDefinition(array $externalFieldset): ?FieldDefinition $possiblePairedField = $prevField->getNext(); } } + if (null === $possiblePairedField && null !== $this->nextField) { $nextField = $externalFieldset[$this->nextField->getName()] ?? null; if (null !== $nextField) { diff --git a/src/Exception/Db/UnknownColumnTypeException.php b/src/Exception/Db/UnknownColumnTypeException.php index f4bda02..36b79a9 100644 --- a/src/Exception/Db/UnknownColumnTypeException.php +++ b/src/Exception/Db/UnknownColumnTypeException.php @@ -21,7 +21,7 @@ class UnknownColumnTypeException extends Exception /** * @var ColumnInterface */ - protected $column; + protected ColumnInterface $column; public function __construct(ColumnInterface $column) { diff --git a/src/FactoryOptions.php b/src/FactoryOptions.php deleted file mode 100644 index e71b44b..0000000 --- a/src/FactoryOptions.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Migrations; - -/** - * Interface for options and data that were generated - */ -interface FactoryOptions -{ - /** - * Set all options to option container - * - * @param array $options - */ - public function setOptions(array $options); - - /** - * Set one option to option container - * - * @param mixed $key - * @param mixed $option - */ - public function setOption($key, $option); - - /** - * Get all options from the option container - * - * @return array - */ - public function getOptions(); - - /** - * Get valid option or throw exception - * - * @param mixed $key - * - * @throw InvalidArgumentException - * - * @return mixed - */ - public function getOption($key); - - /** - * Check whether option container has value with this key - * - * @param mixed $key - * - * @return mixed - */ - public function hasOption($key); - - /** - * Return valid option value or default value - * - * @param mixed $key - * @param mixed $defaultOption - * - * @return mixed - */ - public function getValidOptionOrDefault($key, $defaultOption); -} diff --git a/src/Listeners/DbProfilerListener.php b/src/Listeners/DbProfilerListener.php index 1ed6bd6..9a0d21b 100644 --- a/src/Listeners/DbProfilerListener.php +++ b/src/Listeners/DbProfilerListener.php @@ -21,7 +21,7 @@ */ class DbProfilerListener { - protected $profiler; + protected Profiler $profiler; public function __construct() { diff --git a/src/Migration/Action/Generate.php b/src/Migration/Action/Generate.php index 28ad907..d4ce5c4 100644 --- a/src/Migration/Action/Generate.php +++ b/src/Migration/Action/Generate.php @@ -54,7 +54,7 @@ class Generate /** * @var array */ - protected $supportedColumnTypes = [ + protected array $supportedColumnTypes = [ Column::TYPE_BIGINTEGER => 'TYPE_BIGINTEGER', Column::TYPE_INTEGER => 'TYPE_INTEGER', Column::TYPE_MEDIUMINTEGER => 'TYPE_MEDIUMINTEGER', @@ -91,21 +91,21 @@ class Generate /** * @var array */ - protected $supportedColumnTypesPgsql = [ + protected array $supportedColumnTypesPgsql = [ Column::TYPE_DOUBLE => 'TYPE_FLOAT', ]; /** * @var array */ - protected $supportedColumnTypesMysql = [ + protected array $supportedColumnTypesMysql = [ Column::TYPE_DOUBLE => 'TYPE_DOUBLE', ]; /** * @var array */ - protected $numericColumnTypes = [ + protected array $numericColumnTypes = [ Column::TYPE_INTEGER, Column::TYPE_MEDIUMINTEGER, Column::TYPE_SMALLINTEGER, @@ -118,7 +118,7 @@ class Generate * * @var array */ - protected $noSizeColumnTypes = [ + protected array $noSizeColumnTypes = [ Column::TYPE_DATE, Column::TYPE_DATETIME, @@ -143,7 +143,7 @@ class Generate * * @var array */ - protected $noSizeColumnTypesPostgreSQL = [ + protected array $noSizeColumnTypesPostgreSQL = [ Column::TYPE_BOOLEAN, Column::TYPE_INTEGER, Column::TYPE_BIGINTEGER, @@ -187,21 +187,21 @@ class Generate /** * Table foreign keys and another references * - * @var array|ReferenceInterface[] + * @var ReferenceInterface[] */ - protected $references; + protected array $references; /** * Table options * * @var array */ - protected $options; + protected array $options; /** * @var string|null */ - protected $primaryColumnName = null; + protected ?string $primaryColumnName = null; /** * Numeric columns @@ -210,14 +210,14 @@ class Generate * * @var array */ - protected $numericColumns = []; + protected array $numericColumns = []; /** * Table columns wrapped with "'" single quote symbol * * @var array */ - protected $quoteWrappedColumns = []; + protected array $quoteWrappedColumns = []; /** * Generate constructor. diff --git a/src/Migrations.php b/src/Migrations.php index d5624d3..1df4279 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -111,42 +111,42 @@ public static function isConsole(): bool public static function generate(array $options) { $helper = new Helper(); - $optionStack = new OptionStack(); + $optionStack = new OptionStack($options); $listTables = new ListTablesDb(); - $optionStack->setOptions($options); - $optionStack->setDefaultOption('version', null); - $optionStack->setDefaultOption('descr', null); - $optionStack->setDefaultOption('noAutoIncrement', null); - $optionStack->setDefaultOption('verbose', false); - $optionStack->setDefaultOption('skip-ref-schema', false); + + $optionStack->offsetSetDefault('version'); + $optionStack->offsetSetDefault('descr'); + $optionStack->offsetSetDefault('noAutoIncrement'); + $optionStack->offsetSetDefault('verbose', false); + $optionStack->offsetSetDefault('skip-ref-schema', false); // Migrations directory - $migrationsDirs = $optionStack->getOption('migrationsDir'); + $migrationsDirs = $optionStack->offsetGet('migrationsDir'); $migrationsDir = $helper->getMigrationsDir($migrationsDirs); $versionItem = $optionStack->getVersionNameGeneratingMigration(); - $verbose = $optionStack->getOption('verbose'); - $force = $optionStack->getOption('force'); + $verbose = $optionStack->offsetGet('verbose'); + $force = $optionStack->offsetGet('force'); // Path to migration dir $migrationPath = $helper->getMigrationsPath($versionItem, $migrationsDir, $verbose, $force); // Try to connect to the DB - if (!isset($optionStack->getOption('config')->database)) { + if (!isset($optionStack->offsetGet('config')->database)) { throw new RuntimeException('Cannot load database configuration'); } - ModelMigration::setup($optionStack->getOption('config')->database, $verbose); - ModelMigration::setSkipAutoIncrement((bool) $optionStack->getOption('noAutoIncrement')); + ModelMigration::setup($optionStack->offsetGet('config')->database, $verbose); + ModelMigration::setSkipAutoIncrement((bool) $optionStack->offsetGet('noAutoIncrement')); ModelMigration::setMigrationPath($migrationsDir); $wasMigrated = false; - if ($optionStack->getOption('tableName') === '@') { + if ($optionStack->offsetGet('tableName') === '@') { $migrations = ModelMigration::generateAll( $versionItem, - $optionStack->getOption('exportData'), - $optionStack->getOption('exportDataFromTables') ?: [], - $optionStack->getOption('skip-ref-schema') + $optionStack->offsetGet('exportData'), + $optionStack->offsetGet('exportDataFromTables') ?: [], + $optionStack->offsetGet('skip-ref-schema') ); if (!$verbose) { @@ -160,24 +160,24 @@ public static function generate(array $options) } } } else { - $prefix = $optionStack->getPrefixOption($optionStack->getOption('tableName')); + $prefix = $optionStack->getPrefixOption($optionStack->offsetGet('tableName')); if (!empty($prefix)) { - $optionStack->setOption('tableName', $listTables->listTablesForPrefix($prefix)); + $optionStack->offsetSet('tableName', $listTables->listTablesForPrefix($prefix)); } - if ($optionStack->getOption('tableName') === '') { + if ($optionStack->offsetGet('tableName') === '') { print Color::info('No one table is created. You should create tables first.') . PHP_EOL; return; } - $tables = explode(',', $optionStack->getOption('tableName')); + $tables = explode(',', $optionStack->offsetGet('tableName')); foreach ($tables as $table) { $migration = ModelMigration::generate( $versionItem, $table, - $optionStack->getOption('exportData'), - $optionStack->getOption('exportDataFromTables') ?: [], - $optionStack->getOption('skip-ref-schema') + $optionStack->offsetGet('exportData'), + $optionStack->offsetGet('exportDataFromTables') ?: [], + $optionStack->offsetGet('skip-ref-schema') ); if (!$verbose) { $tableFile = $migrationPath . DIRECTORY_SEPARATOR . $table . '.php'; @@ -208,25 +208,25 @@ public static function generate(array $options) */ public static function run(array $options) { - $optionStack = new OptionStack(); + $optionStack = new OptionStack($options); $listTables = new ListTablesIterator(); - $optionStack->setOptions($options); - $optionStack->setDefaultOption('verbose', false); - $optionStack->setDefaultOption('skip-foreign-checks', false); + + $optionStack->offsetSetDefault('verbose', false); + $optionStack->offsetSetDefault('skip-foreign-checks', false); // Define versioning type to be used - if (!empty($options['tsBased']) || $optionStack->getOption('tsBased')) { + if (!empty($options['tsBased']) || $optionStack->offsetGet('tsBased')) { VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED); } else { VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL); } - if (!$optionStack->getOption('config') instanceof Config) { + if (!$optionStack->offsetGet('config') instanceof Config) { throw new RuntimeException('Internal error. Config should be an instance of ' . Config::class); } // Init ModelMigration - if (!isset($optionStack->getOption('config')->database)) { + if (!isset($optionStack->offsetGet('config')->database)) { throw new RuntimeException('Cannot load database configuration'); } @@ -234,7 +234,7 @@ public static function run(array $options) $initialVersion = self::getCurrentVersion($optionStack->getOptions()); $completedVersions = self::getCompletedVersions($optionStack->getOptions()); $versionItems = []; - $migrationsDirList = $optionStack->getOption('migrationsDir'); + $migrationsDirList = $optionStack->offsetGet('migrationsDir'); if (is_array($migrationsDirList)) { foreach ($migrationsDirList as $migrationsDir) { $migrationsDir = rtrim($migrationsDir, '\\/'); @@ -259,11 +259,11 @@ public static function run(array $options) } $finalVersion = null; - if (isset($options['version']) && $optionStack->getOption('version') !== null) { + if (isset($options['version']) && $optionStack->offsetGet('version') !== null) { $finalVersion = VersionCollection::createItem($options['version']); } - $optionStack->setOption('tableName', $options['tableName'] ?? null, '@'); + $optionStack->offsetSetOrDefault('tableName', $options['tableName'] ?? null, '@'); if (empty($versionItems)) { $migrationsPath = is_array($migrationsDirList) ? @@ -283,7 +283,7 @@ public static function run(array $options) return; } - ModelMigration::setup($optionStack->getOption('config')->database, $optionStack->getOption('verbose')); + ModelMigration::setup($optionStack->offsetGet('config')->database, $optionStack->offsetGet('verbose')); self::connectionSetup($optionStack->getOptions()); /** @@ -320,7 +320,7 @@ public static function run(array $options) // Run migration $versionsBetween = VersionCollection::between($initialVersion, $finalVersion, $versionItems); - $prefix = $optionStack->getPrefixOption($optionStack->getOption('tableName')); + $prefix = $optionStack->getPrefixOption($optionStack->offsetGet('tableName')); /** @var IncrementalItem $versionItem */ foreach ($versionsBetween as $versionItem) { @@ -378,7 +378,7 @@ public static function run(array $options) $migrationStartTime = date('Y-m-d H:i:s'); - if ($optionStack->getOption('tableName') === '@') { + if ($optionStack->offsetGet('tableName') === '@') { /** @var SplFileInfo $fileInfo */ foreach ($iterator as $fileInfo) { if (!$fileInfo->isFile() || 0 !== strcasecmp($fileInfo->getExtension(), 'php')) { @@ -389,21 +389,21 @@ public static function run(array $options) $fileInfo->getBasename('.php'), $initialVersion, $versionItem, - $optionStack->getOption('skip-foreign-checks') + $optionStack->offsetGet('skip-foreign-checks') ); } } else { if (!empty($prefix)) { - $optionStack->setOption('tableName', $listTables->listTablesForPrefix($prefix, $iterator)); + $optionStack->offsetSet('tableName', $listTables->listTablesForPrefix($prefix, $iterator)); } - $tables = explode(',', $optionStack->getOption('tableName')); + $tables = explode(',', $optionStack->offsetGet('tableName')); foreach ($tables as $tableName) { ModelMigration::migrate( $tableName, $initialVersion, $versionItem, - $optionStack->getOption('skip-foreign-checks') + $optionStack->offsetGet('skip-foreign-checks') ); } } @@ -604,8 +604,8 @@ public static function getCurrentVersion(array $options) $query = 'SELECT * FROM ' . self::MIGRATION_LOG_TABLE . ' ORDER BY version DESC LIMIT 1'; $lastGoodMigration = $connection->query($query); - if (0 == $lastGoodMigration->numRows()) { - return VersionCollection::createItem(null); + if (0 === $lastGoodMigration->numRows()) { + return VersionCollection::createItem(); } $lastGoodMigration = $lastGoodMigration->fetchArray(); diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index 449a264..c352f23 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -96,21 +96,21 @@ class Migration * * @var string */ - private static $migrationPath = ''; + private static string $migrationPath = ''; /** * Skip auto increment * * @var bool */ - private static $skipAI = true; + private static bool $skipAI = true; /** * Version of the migration file * * @var string|null */ - protected $version = null; + protected ?string $version = null; /** * Prepares component diff --git a/src/Version/IncrementalItem.php b/src/Version/IncrementalItem.php index 344392c..a6f0b98 100644 --- a/src/Version/IncrementalItem.php +++ b/src/Version/IncrementalItem.php @@ -37,22 +37,22 @@ class IncrementalItem implements ItemInterface /** * @var string */ - private $path; + private string $path; /** * @var string */ - private $version; + private string $version; /** * @var int */ - private $versionStamp = 0; + private int $versionStamp = 0; /** * @var array */ - private $parts; + private array $parts; /** * @param string $version @@ -89,9 +89,9 @@ public function __construct(string $version, int $numberParts = 3) * * @return null|IncrementalItem */ - public static function maximum(array $versions) + public static function maximum(array $versions): ?IncrementalItem { - if (count($versions) == 0) { + if (count($versions) === 0) { return null; } @@ -117,15 +117,15 @@ public static function sortDesc(array $versions): array } /** - * Allows to check whether a version is in a range between two values. + * Allows checking whether a version is in a range between two values. * * @param IncrementalItem|string $initialVersion * @param IncrementalItem|string $finalVersion - * @param ItemInterface[] $versions + * @param ItemInterface[] $versions * * @return ItemInterface[] */ - public static function between($initialVersion, $finalVersion, $versions) + public static function between($initialVersion, $finalVersion, array $versions): array { $versions = self::sortAsc($versions); @@ -191,7 +191,7 @@ public function getStamp(): int * * @return IncrementalItem */ - public function addMinor(int $number) + public function addMinor(int $number): IncrementalItem { $parts = array_reverse($this->parts); if (isset($parts[0])) { @@ -241,12 +241,12 @@ public function getPath(): string * * @param string $path */ - public function setPath($path): void + public function setPath(string $path): void { $this->path = $path; } - protected function regenerateVersionStamp() + protected function regenerateVersionStamp(): IncrementalItem { $n = 2; $versionStamp = 0; @@ -266,7 +266,7 @@ protected function regenerateVersionStamp() return $this; } - protected function setParts(array $parts) + protected function setParts(array $parts): IncrementalItem { $this->parts = array_map(function ($v) { return strval($v); diff --git a/src/Version/ItemCollection.php b/src/Version/ItemCollection.php index 24d43c4..b507c3b 100644 --- a/src/Version/ItemCollection.php +++ b/src/Version/ItemCollection.php @@ -21,7 +21,7 @@ use function preg_match; /** - * The item collection lets you to work with an abstract ItemInterface. + * The item collection lets you work with an abstract ItemInterface. */ class ItemCollection { @@ -42,14 +42,14 @@ class ItemCollection /** * @var int */ - public static $type = self::TYPE_INCREMENTAL; + public static int $type = self::TYPE_INCREMENTAL; /** * Set collection type * * @param int $type */ - public static function setType($type) + public static function setType(int $type) { self::$type = $type; } @@ -61,7 +61,7 @@ public static function setType($type) * * @return IncrementalItem|TimestampedItem */ - public static function createItem($version = null) + public static function createItem(string $version = null) { if (self::TYPE_INCREMENTAL === self::$type) { $version = $version ?: '0.0.0'; @@ -99,17 +99,17 @@ public static function isCorrectVersion(string $version): bool * * @param array $versions * - * @return null|ItemInterface|IncrementalItem + * @return ItemInterface|null */ - public static function maximum(array $versions) + public static function maximum(array $versions): ?ItemInterface { - if (count($versions) == 0) { + if (count($versions) === 0) { return null; } $versions = self::sortDesc($versions); - return $versions[0]; + return $versions[0] ?? null; } /** diff --git a/src/Version/TimestampedItem.php b/src/Version/TimestampedItem.php index d189e06..156ded6 100644 --- a/src/Version/TimestampedItem.php +++ b/src/Version/TimestampedItem.php @@ -28,22 +28,22 @@ class TimestampedItem implements ItemInterface /** * @var string */ - protected $version; + protected string $version; /** - * @var boolean + * @var bool */ - protected $isFullVersion; + protected bool $isFullVersion; /** * @var array */ - protected $parts = []; + protected array $parts = []; /** * @var string */ - private $path; + private string $path = ''; /** * @param string $version String representation of the version diff --git a/tests/unit/Console/OptionStackTest.php b/tests/unit/Console/OptionStackTest.php index 04a5897..6253381 100644 --- a/tests/unit/Console/OptionStackTest.php +++ b/tests/unit/Console/OptionStackTest.php @@ -14,23 +14,10 @@ namespace Phalcon\Migrations\Tests\Unit\Console; use Codeception\Test\Unit; -use Phalcon\Migrations\Console\OptionParserTrait; use Phalcon\Migrations\Console\OptionStack; final class OptionStackTest extends Unit { - use OptionParserTrait; - - /** - * @var OptionStack - */ - public $options; - - public function setUp(): void - { - $this->options = new OptionStack(); - } - /** * @see testSetOptionAndGetOption11 */ @@ -38,7 +25,7 @@ public function setOptionAndGetOption11DataProvider(): array { return [ ['foo-bar', 'bar-foo', 'foo-bar'], - ['', 'bar-foo', 'bar-foo'], + [null, 'bar-foo', 'bar-foo'], ]; } @@ -55,14 +42,14 @@ public function setDefaultOptionIfOptionDidntExistDataProvider(): array public function testGetAndSetOptions(): void { - $options = [ + $data = [ 'test' => 'foo', 'test2' => 'bar', ]; - $this->options->setOptions($options); + $options = new OptionStack($data); - $this->assertSame($options, $this->options->getOptions()); + $this->assertSame($data, $options->getOptions()); } /** @@ -75,9 +62,10 @@ public function testGetAndSetOptions(): void public function testSetOptionAndGetOption11($option, $defaultValue, $expected): void { $key = 'set-test'; - $this->options->setOption($key, $option, $defaultValue); + $options = new OptionStack(); + $options->offsetSetOrDefault($key, $option, $defaultValue); - $this->assertSame($expected, $this->options->getOption($key)); + $this->assertSame($expected, $options->offsetGet($key)); } /** @@ -89,44 +77,27 @@ public function testSetOptionAndGetOption11($option, $defaultValue, $expected): */ public function testSetDefaultOptionIfOptionDidntExist($key, $defaultValue, $expected): void { - $this->options->setOption('test', 'bar'); - $this->options->setDefaultOption($key, $defaultValue); + $options = new OptionStack(); - $this->assertSame($expected, $this->options->getOption($key)); - } - - public function testCheckingReceivedOption(): void - { - $this->options->setOption('true-option', 'foo-bar'); - - $option1 = $this->options->isReceivedOption('true-option'); - $option2 = $this->options->isReceivedOption('false-option'); - - $this->assertTrue($option1); - $this->assertFalse($option2); - } - - public function testReturnValidOptionOrSetDefault(): void - { - $this->options->setOptions(['test' => 'foo', 'test2' => 'bar']); + $options->offsetSet('test', 'bar'); + $options->offsetSetDefault($key, $defaultValue); - $this->assertSame('foo', $this->options->getValidOption('test', 'bar')); - $this->assertSame('bar', $this->options->getValidOption('false-option', 'bar')); + $this->assertSame($expected, $options->offsetGet($key)); } public function testReturnPrefixFromOptionWithoutSetPrefix(): void { - $this->options->setOptions(['test' => 'foo', 'test2' => 'bar']); + $options = new OptionStack(['test' => 'foo', 'test2' => 'bar']); - $this->assertSame('foo', $this->options->getPrefixOption('foo*')); - $this->assertSame('bar', $this->options->getPrefixOption('bar*')); + $this->assertSame('foo', $options->getPrefixOption('foo*')); + $this->assertSame('bar', $options->getPrefixOption('bar*')); } public function testReturnPrefixFromOptionWithSetPrefix(): void { - $this->options->setOptions(['test' => 'foo', 'test2' => 'bar']); + $options = new OptionStack(['test' => 'foo', 'test2' => 'bar']); - $this->assertSame('foo', $this->getPrefixOption('foo^', '^')); - $this->assertSame('bar', $this->getPrefixOption('bar?', '?')); + $this->assertSame('foo', $options->getPrefixOption('foo^', '^')); + $this->assertSame('bar', $options->getPrefixOption('bar?', '?')); } }