Skip to content

Commit

Permalink
Fix PHPStan findings
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Nov 7, 2020
1 parent 96ce53a commit 1da8a2d
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/Migrations/MigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function getRelativeVersion(string $version, int $delta) : ?string
return null;
}

$relativeVersion = ((int) $offset) + $delta;
$relativeVersion = $offset + $delta;

if (! isset($versions[$relativeVersion])) {
// Unknown version or delta out of bounds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use function assert;
use function escapeshellarg;
use function proc_open;
use function str_repeat;
Expand Down Expand Up @@ -111,8 +112,10 @@ protected function getMigrationConfiguration(
) : Configuration {
if ($this->migrationConfiguration === null) {
if ($this->hasConfigurationHelper()) {
$helperSet = $this->getHelperSet();
assert($helperSet !== null);
/** @var ConfigurationHelper $configHelper */
$configHelper = $this->getHelperSet()->get('configuration');
$configHelper = $helperSet->get('configuration');
} else {
$configHelper = new ConfigurationHelper(
$this->getConnection($input),
Expand Down Expand Up @@ -187,8 +190,10 @@ private function hasConfigurationHelper() : bool
private function getConnection(InputInterface $input) : Connection
{
if ($this->connection === null) {
$helperSet = $this->getHelperSet();
assert($helperSet !== null);
$this->connection = (new ConnectionLoader($this->configuration))
->getConnection($input, $this->getHelperSet());
->getConnection($input, $helperSet);
}

return $this->connection;
Expand Down
18 changes: 13 additions & 5 deletions lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use const FILTER_VALIDATE_BOOLEAN;
use function assert;
use function class_exists;
use function filter_var;
use function is_array;
use function is_bool;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -111,11 +115,14 @@ public function execute(
OutputInterface $output
) : ?int {
$filterExpression = $input->getOption('filter-expression') ?? null;
$formatted = (bool) $input->getOption('formatted');
$lineLength = (int) $input->getOption('line-length');
$allowEmptyDiff = (bool) $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$fromEmptySchema = (bool) $input->getOption('from-empty-schema');
assert(is_string($filterExpression) || $filterExpression === null);
$formatted = (bool) $input->getOption('formatted');
$lineLength = $input->getOption('line-length');
assert(! is_array($lineLength) && ! is_bool($lineLength));
$lineLength = (int) $lineLength;
$allowEmptyDiff = (bool) $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$fromEmptySchema = (bool) $input->getOption('from-empty-schema');

if ($formatted) {
if (! class_exists('SqlFormatter')) {
Expand Down Expand Up @@ -146,6 +153,7 @@ public function execute(
}

$editorCommand = $input->getOption('editor-cmd');
assert(is_string($editorCommand) || $editorCommand === null);

if ($editorCommand !== null) {
$this->procOpen($editorCommand, $path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function class_exists;
use function count;
use function is_array;
use function is_bool;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -68,7 +72,9 @@ public function execute(
OutputInterface $output
) : ?int {
$formatted = (bool) $input->getOption('formatted');
$lineLength = (int) $input->getOption('line-length');
$lineLength = $input->getOption('line-length');
assert(! is_array($lineLength) && ! is_bool($lineLength));
$lineLength = (int) $lineLength;

$schemaDumper = $this->dependencyFactory->getSchemaDumper();
$versions = $this->migrationRepository->getVersions();
Expand All @@ -94,6 +100,7 @@ public function execute(
);

$editorCommand = $input->getOption('editor-cmd');
assert(is_string($editorCommand) || $editorCommand === null);

if ($editorCommand !== null) {
$this->procOpen($editorCommand, $path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function getcwd;
use function is_string;

/**
* The ExecutCommand class is responsible for executing a single migration version up or down.
Expand Down Expand Up @@ -92,7 +94,8 @@ protected function configure() : void

public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$version = $input->getArgument('version');
$version = $input->getArgument('version');
assert(is_string($version));
$timeAllQueries = (bool) $input->getOption('query-time');
$dryRun = (bool) $input->getOption('dry-run');
$path = $input->getOption('write-sql');
Expand All @@ -104,6 +107,7 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int

if ($path !== false) {
$path = $path ?? getcwd();
assert(is_string($path));

$version->writeSqlFile($path, $direction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -51,6 +53,7 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
$path = $migrationGenerator->generateMigration($versionNumber);

$editorCommand = $input->getOption('editor-cmd');
assert(is_string($editorCommand) || $editorCommand === null);

if ($editorCommand !== null) {
$this->procOpen($editorCommand, $path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function count;
use function getcwd;
use function is_string;
use function sprintf;
use function substr;

Expand Down Expand Up @@ -115,7 +117,8 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$this->outputHeader($output);

$version = (string) $input->getArgument('version');
$version = $input->getArgument('version');
assert(is_string($version));
$path = $input->getOption('write-sql');
$allowNoMigration = (bool) $input->getOption('allow-no-migration');
$timeAllQueries = (bool) $input->getOption('query-time');
Expand All @@ -142,6 +145,7 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
if ($path !== false) {
$path = $path ?? getcwd();

assert(is_string($path));
$migrator->writeSqlFile($path, $version);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -128,6 +130,8 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
private function markVersions(InputInterface $input, OutputInterface $output) : void
{
$affectedVersion = $input->getArgument('version');
assert(is_string($affectedVersion) || $affectedVersion === null);

$allOption = $input->getOption('all');
$rangeFromOption = $input->getOption('range-from');
$rangeToOption = $input->getOption('range-to');
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface;
use function assert;
use function is_string;

/**
* The ConnectionLoader class is responsible for loading the Doctrine\DBAL\Connection instance to use for migrations.
Expand Down Expand Up @@ -46,8 +48,11 @@ protected function createConnectionConfigurationChainLoader(
InputInterface $input,
HelperSet $helperSet
) : ConnectionLoaderInterface {
$dbConfiguration = $input->getOption('db-configuration');
assert(is_string($dbConfiguration) || $dbConfiguration === null);

return new ConnectionConfigurationChainLoader([
new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')),
new ArrayConnectionConfigurationLoader($dbConfiguration),
new ArrayConnectionConfigurationLoader('migrations-db.php'),
new ConnectionHelperLoader($helperSet, 'connection'),
new ConnectionConfigurationLoader($this->configuration),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputInterface;
use function assert;
use function file_exists;
use function is_string;
use function pathinfo;

/**
Expand Down Expand Up @@ -45,6 +47,8 @@ public function getMigrationConfig(InputInterface $input) : Configuration
$configuration = $input->getOption('configuration');

if ($configuration !== null) {
assert(is_string($configuration));

return $this->loadConfig($configuration);
}

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parameters:
- '~ProxyManager\\Proxy\\VirtualProxyInterface~'
- '~^Parameter #1 \$files of method Doctrine\\Migrations\\Finder\\Finder::loadMigrationClasses\(\) expects array<string>, array<int, string\|false> given\.\z~'
- '~^Class Doctrine\\Migrations\\Tests\\DoesNotExistAtAll not found\.\z~'
- '~^Call to method getVersion\(\) of deprecated class PackageVersions\\Versions\.$~'

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
Expand Down
4 changes: 4 additions & 0 deletions tests/Doctrine/Migrations/Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ public function testMigrateWithConnectionWithAutoCommitOffStillPersistsChanges()
{
$listener = new AutoCommitListener();
[$conn, $config] = self::fileConnectionAndConfig();
assert($conn instanceof Connection);
assert($config instanceof Configuration);
$config->registerMigration('1', MigrateWithDataModification::class);
$migrator = $this->createTestMigrator($config);
$conn->getEventManager()->addEventSubscriber($listener);
Expand Down Expand Up @@ -552,6 +554,8 @@ public function testMigrationExecutedAt() : void

/**
* @return Connection[]|Configuration[]
*
* @psalm-return array{Connection,Configuration}
*/
private static function fileConnectionAndConfig() : array
{
Expand Down
4 changes: 3 additions & 1 deletion tests/Doctrine/Migrations/Tests/MigrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public function getOutputStreamContent(StreamOutput $streamOutput) : string
{
$stream = $streamOutput->getStream();
rewind($stream);
$contents = stream_get_contents($stream);
assert($contents !== false);

return stream_get_contents($stream);
return $contents;
}

/** @return resource */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\Migrations\Tests\Provider;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseMetadataFactoryAlias;
use Doctrine\Persistence\Mapping\ClassMetadata;
use function array_reverse;

class ClassMetadataFactory extends BaseMetadataFactoryAlias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function invokeMigrationConfigurationGetter(
}

if (! $noConnection) {
$command->getHelperSet()->set(
$helperSet = $command->getHelperSet();
assert($helperSet !== null);
$helperSet->set(
new ConnectionHelper($this->getSqliteConnection()),
'connection'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use function array_replace;
use function assert;
use function is_string;

abstract class CommandTestCase extends MigrationTestCase
{
Expand Down Expand Up @@ -47,7 +49,10 @@ protected function createConnection() : Connection

protected function createCommandTester() : CommandTester
{
return new CommandTester($this->app->find($this->command->getName()));
$name = $this->command->getName();
assert(is_string($name));

return new CommandTester($this->app->find($name));
}

/**
Expand Down

0 comments on commit 1da8a2d

Please sign in to comment.