Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to codebase #1995

Merged
merged 28 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
937871c
starting adding types to codebase
MasterOdin Jun 17, 2021
3cfd71a
more typing
MasterOdin Jun 17, 2021
88bc125
replace rest of types
MasterOdin Jun 19, 2021
761495d
fix types in tests
MasterOdin Jun 19, 2021
0baf356
fix type hint on Config class
MasterOdin Jun 19, 2021
6ef2a2c
replace mixed with concrete types
MasterOdin Jun 19, 2021
df0901f
fix typehint on NullGenerator
MasterOdin Jun 19, 2021
f8d4391
return ConfigInterface not Config
MasterOdin Jun 19, 2021
f385bbb
fix tests
MasterOdin Jun 19, 2021
f8f1aad
fix phpstan errors
MasterOdin Jun 19, 2021
1296fee
getValues can be null
MasterOdin Jun 19, 2021
a3e86e7
more things can be null
MasterOdin Jun 19, 2021
cfb4f9f
more nulls
MasterOdin Jun 19, 2021
2e4b1e8
fix accidental test failures
MasterOdin Jun 19, 2021
223d553
more nulls
MasterOdin Jun 19, 2021
166b310
convert int to float for Column::
MasterOdin Jun 19, 2021
0dd2a06
fix int/float typing
MasterOdin Jun 20, 2021
5978259
remove typing on RawBufferedOutput
MasterOdin Jun 20, 2021
5dbfd1e
parseDefault may receive null
MasterOdin Jun 20, 2021
32d7368
Update src/Phinx/Db/Adapter/AdapterFactory.php
MasterOdin Jul 28, 2021
8d608dd
Apply suggestions from code review
MasterOdin Jul 28, 2021
be536ff
more review comment addressing
MasterOdin Jul 28, 2021
1d638fe
add inline type hint for phpstan
MasterOdin Jul 28, 2021
f7d6a2f
review comment
MasterOdin Jul 28, 2021
2c9a111
Merge branch '0.next' into add_typing
MasterOdin Aug 3, 2021
d15e3da
Update phpstan-baseline.neon
MasterOdin Aug 3, 2021
468c283
Fix limit type hint back to int
MasterOdin Aug 3, 2021
5222f12
missed a test
MasterOdin Aug 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 41 additions & 41 deletions src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class Config implements ConfigInterface, NamespaceAwareInterface
protected $values = [];

/**
* @var string
* @var string|null
*/
protected $configFilePath;

/**
* @param array $configArray Config array
* @param string|null $configFilePath Config file path
*/
public function __construct(array $configArray, $configFilePath = null)
public function __construct(array $configArray, ?string $configFilePath = null)
{
$this->configFilePath = $configFilePath;
$this->values = $this->replaceTokens($configArray);
Expand All @@ -60,9 +60,9 @@ public function __construct(array $configArray, $configFilePath = null)
*
* @param string $configFilePath Path to the Yaml File
* @throws \RuntimeException
* @return \Phinx\Config\Config
* @return \Phinx\Config\ConfigInterface
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function fromYaml($configFilePath)
public static function fromYaml(string $configFilePath): ConfigInterface
{
if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) {
// @codeCoverageIgnoreStart
Expand All @@ -88,9 +88,9 @@ public static function fromYaml($configFilePath)
*
* @param string $configFilePath Path to the JSON File
* @throws \RuntimeException
* @return \Phinx\Config\Config
* @return \Phinx\Config\ConfigInterface
*/
public static function fromJson($configFilePath)
public static function fromJson(string $configFilePath): ConfigInterface
{
if (!function_exists('json_decode')) {
// @codeCoverageIgnoreStart
Expand All @@ -114,9 +114,9 @@ public static function fromJson($configFilePath)
*
* @param string $configFilePath Path to the PHP File
* @throws \RuntimeException
* @return \Phinx\Config\Config
* @return \Phinx\Config\ConfigInterface
*/
public static function fromPhp($configFilePath)
public static function fromPhp(string $configFilePath): ConfigInterface
{
ob_start();
/** @noinspection PhpIncludeInspection */
Expand All @@ -138,7 +138,7 @@ public static function fromPhp($configFilePath)
/**
* @inheritDoc
*/
public function getEnvironments()
public function getEnvironments(): ?array
{
if (isset($this->values) && isset($this->values['environments'])) {
$environments = [];
Expand All @@ -157,7 +157,7 @@ public function getEnvironments()
/**
* @inheritDoc
*/
public function getEnvironment($name)
public function getEnvironment(string $name): ?array
{
$environments = $this->getEnvironments();

Expand Down Expand Up @@ -187,15 +187,15 @@ public function getEnvironment($name)
/**
* @inheritDoc
*/
public function hasEnvironment($name)
public function hasEnvironment(string $name): bool
{
return $this->getEnvironment($name) !== null;
}

/**
* @inheritDoc
*/
public function getDefaultEnvironment()
public function getDefaultEnvironment(): string
{
// The $PHINX_ENVIRONMENT variable overrides all other default settings
$env = getenv('PHINX_ENVIRONMENT');
Expand Down Expand Up @@ -242,23 +242,23 @@ public function getDefaultEnvironment()
/**
* @inheritDoc
*/
public function getAlias($alias)
public function getAlias($alias): ?string
{
return !empty($this->values['aliases'][$alias]) ? $this->values['aliases'][$alias] : null;
}

/**
* @inheritDoc
*/
public function getAliases()
public function getAliases(): array
{
return !empty($this->values['aliases']) ? $this->values['aliases'] : [];
}

/**
* @inheritDoc
*/
public function getConfigFilePath()
public function getConfigFilePath(): ?string
{
return $this->configFilePath;
}
Expand All @@ -267,7 +267,7 @@ public function getConfigFilePath()
* @inheritDoc
* @throws \UnexpectedValueException
*/
public function getMigrationPaths()
public function getMigrationPaths(): array
{
if (!isset($this->values['paths']['migrations'])) {
throw new UnexpectedValueException('Migrations path missing from config file');
Expand All @@ -280,24 +280,11 @@ public function getMigrationPaths()
return $this->values['paths']['migrations'];
}

/**
* Gets the base class name for migrations.
*
* @param bool $dropNamespace Return the base migration class name without the namespace.
* @return string
*/
public function getMigrationBaseClassName($dropNamespace = true)
{
$className = !isset($this->values['migration_base_class']) ? 'Phinx\Migration\AbstractMigration' : $this->values['migration_base_class'];

return $dropNamespace ? (substr(strrchr($className, '\\'), 1) ?: $className) : $className;
}

/**
* @inheritDoc
* @throws \UnexpectedValueException
*/
public function getSeedPaths()
public function getSeedPaths(): array
{
if (!isset($this->values['paths']['seeds'])) {
throw new UnexpectedValueException('Seeds path missing from config file');
Expand All @@ -310,13 +297,26 @@ public function getSeedPaths()
return $this->values['paths']['seeds'];
}

/**
* Gets the base class name for migrations.
*
* @param bool $dropNamespace Return the base migration class name without the namespace.
* @return string
*/
public function getMigrationBaseClassName(bool $dropNamespace = true): string
{
$className = !isset($this->values['migration_base_class']) ? 'Phinx\Migration\AbstractMigration' : $this->values['migration_base_class'];

return $dropNamespace ? (substr(strrchr($className, '\\'), 1) ?: $className) : $className;
}

/**
* Gets the base class name for seeders.
*
* @param bool $dropNamespace Return the base seeder class name without the namespace.
* @return string
*/
public function getSeedBaseClassName($dropNamespace = true)
public function getSeedBaseClassName(bool $dropNamespace = true): string
{
$className = !isset($this->values['seed_base_class']) ? 'Phinx\Seed\AbstractSeed' : $this->values['seed_base_class'];

Expand Down Expand Up @@ -354,7 +354,7 @@ public function getTemplateClass()
/**
* {@inheritdoc}
*/
public function getDataDomain()
public function getDataDomain(): array
{
if (!isset($this->values['data_domain'])) {
return [];
Expand All @@ -366,7 +366,7 @@ public function getDataDomain()
/**
* @inheritDoc
*/
public function getContainer()
public function getContainer(): ?\Psr\Container\ContainerInterface
MasterOdin marked this conversation as resolved.
Show resolved Hide resolved
{
if (!isset($this->values['container'])) {
return null;
Expand All @@ -380,7 +380,7 @@ public function getContainer()
*
* @return string
*/
public function getVersionOrder()
public function getVersionOrder(): string
{
if (!isset($this->values['version_order'])) {
return self::VERSION_ORDER_CREATION_TIME;
Expand All @@ -394,7 +394,7 @@ public function getVersionOrder()
*
* @return bool
*/
public function isVersionOrderCreationTime()
public function isVersionOrderCreationTime(): bool
{
$versionOrder = $this->getVersionOrder();

Expand All @@ -421,7 +421,7 @@ public function getBootstrapFile()
* @param array $arr Array to replace
* @return array
*/
protected function replaceTokens(array $arr)
protected function replaceTokens(array $arr): array
{
// Get environment variables
// Depending on configuration of server / OS and variables_order directive,
Expand Down Expand Up @@ -449,7 +449,7 @@ protected function replaceTokens(array $arr)
* @param string[] $tokens Array of tokens to search for
* @return array
*/
protected function recurseArrayForTokens($arr, $tokens)
protected function recurseArrayForTokens(array $arr, array $tokens): array
{
$out = [];
foreach ($arr as $name => $value) {
Expand All @@ -476,7 +476,7 @@ protected function recurseArrayForTokens($arr, $tokens)
* @param array $options Options
* @return array
*/
protected function parseAgnosticDsn(array $options)
protected function parseAgnosticDsn(array $options): array
{
$parsed = Util::parseDsn($options['dsn'] ?? '');
if ($parsed) {
Expand All @@ -495,7 +495,7 @@ protected function parseAgnosticDsn(array $options)
* @param mixed $value Value
* @return void
*/
public function offsetSet($id, $value)
public function offsetSet($id, $value): void
{
$this->values[$id] = $value;
}
Expand All @@ -522,7 +522,7 @@ public function offsetGet($id)
* @param mixed $id ID
* @return bool
*/
public function offsetExists($id)
public function offsetExists($id): bool
{
return isset($this->values[$id]);
}
Expand All @@ -533,7 +533,7 @@ public function offsetExists($id)
* @param mixed $id ID
* @return void
*/
public function offsetUnset($id)
public function offsetUnset($id): void
{
unset($this->values[$id]);
}
Expand Down
30 changes: 15 additions & 15 deletions src/Phinx/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface ConfigInterface extends ArrayAccess
*
* @return array|null
*/
public function getEnvironments();
public function getEnvironments(): ?array;

/**
* Returns the configuration for a given environment.
Expand All @@ -35,59 +35,59 @@ public function getEnvironments();
* @param string $name Environment Name
* @return array|null
*/
public function getEnvironment($name);
public function getEnvironment(string $name): ?array;

/**
* Does the specified environment exist in the configuration file?
*
* @param string $name Environment Name
* @return bool
*/
public function hasEnvironment($name);
public function hasEnvironment(string $name): bool;

/**
* Gets the default environment name.
*
* @throws \RuntimeException
* @return string
*/
public function getDefaultEnvironment();
public function getDefaultEnvironment(): string;

/**
* Get the aliased value from a supplied alias.
*
* @param string $alias Alias
* @return string|null
*/
public function getAlias($alias);
public function getAlias(string $alias): ?string;

/**
* Get all the aliased values.
*
* @return string[]
*/
public function getAliases();
public function getAliases(): array;

/**
* Gets the config file path.
*
* @return string
*/
public function getConfigFilePath();
public function getConfigFilePath(): ?string;

/**
* Gets the paths to search for migration files.
*
* @return string[]
*/
public function getMigrationPaths();
public function getMigrationPaths(): array;

/**
* Gets the paths to search for seed files.
*
* @return string[]
*/
public function getSeedPaths();
public function getSeedPaths(): array;

/**
* Get the template file name.
Expand All @@ -108,28 +108,28 @@ public function getTemplateClass();
*
* @return \Psr\Container\ContainerInterface|null
*/
public function getContainer();
public function getContainer(): ?\Psr\Container\ContainerInterface;
MasterOdin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the data domain array.
*
* @return array
*/
public function getDataDomain();
public function getDataDomain(): array;

/**
* Get the version order.
*
* @return string
*/
public function getVersionOrder();
public function getVersionOrder(): string;

/**
* Is version order creation time?
*
* @return bool
*/
public function isVersionOrderCreationTime();
public function isVersionOrderCreationTime(): bool;

/**
* Get the bootstrap file path
Expand All @@ -144,13 +144,13 @@ public function getBootstrapFile();
* @param bool $dropNamespace Return the base migration class name without the namespace.
* @return string
*/
public function getMigrationBaseClassName($dropNamespace = true);
public function getMigrationBaseClassName(bool $dropNamespace = true): string;

/**
* Gets the base class name for seeders.
*
* @param bool $dropNamespace Return the base seeder class name without the namespace.
* @return string
*/
public function getSeedBaseClassName($dropNamespace = true);
public function getSeedBaseClassName(bool $dropNamespace = true): string;
}