Skip to content

Commit

Permalink
phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Sep 24, 2019
1 parent a8a17e9 commit 473d1bb
Show file tree
Hide file tree
Showing 30 changed files with 119 additions and 104 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"doctrine/orm": "^2.6",
"jdorn/sql-formatter": "^1.1",
"mikey179/vfsStream": "^1.6",
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-deprecation-rules": "^0.10",
"phpstan/phpstan-phpunit": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-deprecation-rules": "^0.11",
"phpstan/phpstan-phpunit": "^0.11",
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan-symfony": "^0.11.6",
"phpunit/phpunit": "^7.0",
"symfony/process": "^3.4||^4.0",
"symfony/yaml": "^3.4||^4.0"
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/Migrations/Configuration/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ private static function applyConfigs(array $configMap, $object, array $data) : v
if (is_array($configMap[$configurationKey])) {
if ($configurationKey === 'table_storage') {
$storageConfig = new TableMetadataStorageConfiguration();
assert($object instanceof Configuration);
$object->setMetadataStorageConfiguration($storageConfig);
self::applyConfigs($configMap[$configurationKey], $storageConfig, $configurationValue);
}
} else {
$callable = $configMap[$configurationKey] instanceof Closure
? $configMap[$configurationKey]
: [$object, $configMap[$configurationKey]];
assert(is_callable($callable));
call_user_func(
$configMap[$configurationKey] instanceof Closure ? $configMap[$configurationKey] : [$object, $configMap[$configurationKey]],
$callable,
$configurationValue,
$object,
$data
Expand Down
49 changes: 27 additions & 22 deletions lib/Doctrine/Migrations/Configuration/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@ class XmlFileLoader extends AbstractFileLoader
/**
* @return mixed[]
*/
private function extractParameters(SimpleXMLElement $root, bool $nodes = true) : array
private function extractParameters(SimpleXMLElement $root, bool $loopOverNodes = true): array
{
$config = [];
foreach ($nodes ? $root->children() : $root->attributes() as $node) {

$itemsToCheck = $loopOverNodes ? $root->children() : $root->attributes();

if (!($itemsToCheck instanceof SimpleXMLElement)){
return $config;
}
foreach ($itemsToCheck as $node) {
$nodeName = strtr($node->getName(), '-', '_');
if ($nodeName === 'migrations_paths') {
$config['migrations_paths'] = [];
foreach ($node->{'path'} as $pathNode) {
$config['migrations_paths'][(string) $pathNode['namespace']] = (string) $pathNode;
$config['migrations_paths'][(string)$pathNode['namespace']] = (string)$pathNode;
}
} elseif ($nodeName === 'storage' && $node->{'table-storage'} instanceof SimpleXMLElement) {
$config['table_storage'] = $this->extractParameters($node->{'table-storage'}, false);
} else {
$config[$nodeName] = (string) $node;
$config[$nodeName] = (string)$node;
}
}

return $config;
}

/**
* @param mixed $file
*/
public function __construct(?ArrayLoader $arrayLoader = null)
{
$this->arrayLoader = $arrayLoader ?: new ArrayLoader();
Expand All @@ -58,32 +61,34 @@ public function __construct(?ArrayLoader $arrayLoader = null)
/**
* @param mixed|string $file
*/
public function load($file) : Configuration
public function load($file): Configuration
{
if (! file_exists($file)) {
if (!file_exists($file)) {
throw FileNotFound::new();
}
libxml_use_internal_errors(true);
try {
libxml_use_internal_errors(true);

$xml = new DOMDocument();
$xml = new DOMDocument();

if ($xml->load($file) === false) {
throw XmlNotValid::malformed();
}
if ($xml->load($file) === false) {
throw XmlNotValid::malformed();
}

$xsdPath = __DIR__ . DIRECTORY_SEPARATOR . 'XML' . DIRECTORY_SEPARATOR . 'configuration.xsd';
// @todo restore validation
// if (! $xml->schemaValidate($xsdPath)) {
// libxml_clear_errors();
//
// throw XmlNotValid::failedValidation();
// }
$xsdPath = __DIR__ . DIRECTORY_SEPARATOR . 'XML' . DIRECTORY_SEPARATOR . 'configuration.xsd';

if ($xml->schemaValidate($xsdPath) === false) {
throw XmlNotValid::failedValidation();
}
} finally {
libxml_clear_errors();
libxml_use_internal_errors(false);
}
$rawXML = file_get_contents($file);
assert($rawXML !== false);

$root = simplexml_load_string($rawXML, SimpleXMLElement::class, LIBXML_NOCDATA);
assert($xml !== false);
assert($root !== false);

$config = $this->extractParameters($root);

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Finder/GlobFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
final class GlobFinder extends Finder
{
/**
* @return string[]
* {@inheritDoc}
*/
public function findMigrations(string $directory, ?string $namespace = null) : array
{
$dir = $this->getRealPath($directory);

$files = glob(rtrim($dir, '/') . '/Version*.php');
$files = glob(rtrim($dir, '/') . '/Version*.php') ?: [];

return $this->loadMigrations($files, $namespace);
}
Expand Down
4 changes: 0 additions & 4 deletions lib/Doctrine/Migrations/Generator/FileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function sprintf;

/**
Expand All @@ -16,9 +15,6 @@
*/
final class FileBuilder implements FileBuilderInterface
{
/** @var AbstractPlatform */
private $platform;

/** @param string[][] $queriesByVersion */
public function buildMigrationFile(
array $queriesByVersion,
Expand Down
7 changes: 2 additions & 5 deletions lib/Doctrine/Migrations/Metadata/AvailableMigrationsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ public function getLast(int $offset = 0) : AvailableMigration
{
$offset = count($this->items)-1-(-1*$offset);
if (! isset($this->items[$offset])) {
throw NoMigrationsFoundWithCriteria::new('first' . ($offset>0 ? ('+' . $offset) : ''));
throw NoMigrationsFoundWithCriteria::new('last' . ($offset>0 ? ('+' . $offset) : ''));
}

return $this->items[$offset];
}

/**
* @return int
*/
public function count()
public function count() : int
{
return count($this->items);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/Migrations/Metadata/ExecutedMigrationsSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function getLast(int $offset = 0) : ExecutedMigration
return $this->items[$offset];
}

/**
* @return int
*/
public function count()
public function count() : int
{
return count($this->items);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/Migrations/Metadata/MigrationPlanList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public function __construct(array $items, string $direction)
$this->direction = $direction;
}

/**
* @return int
*/
public function count()
public function count() : int
{
return count($this->items);
}
Expand Down
15 changes: 8 additions & 7 deletions lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ protected function configure() : void
->addOption(
'editor-cmd',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_REQUIRED,
'Open file with this command upon creation.'
)
->addOption(
'filter-expression',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_REQUIRED,
'Tables which are filtered by Regular Expression.'
)
->addOption(
Expand All @@ -60,7 +60,7 @@ protected function configure() : void
->addOption(
'line-length',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_REQUIRED,
'Max line length of unformatted lines.',
120
)
Expand All @@ -69,7 +69,7 @@ protected function configure() : void
null,
InputOption::VALUE_OPTIONAL,
'Check Database Platform to the generated code.',
true
1
);
}

Expand All @@ -80,10 +80,10 @@ public function execute(
InputInterface $input,
OutputInterface $output
) : ?int {
$filterExpression = $input->getOption('filter-expression') ?? null;
$formatted = (bool) $input->getOption('formatted');
$lineLength = (int) $input->getOption('line-length');
$filterExpression = (string)$input->getOption('filter-expression') ?: null;
$lineLength = (int)$input->getOption('line-length');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$formatted = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN);

if ($formatted) {
if (! class_exists('SqlFormatter')) {
Expand All @@ -107,6 +107,7 @@ public function execute(
$editorCommand = $input->getOption('editor-cmd');

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function execute(
InputInterface $input,
OutputInterface $output
) : ?int {
$formatted = (bool) $input->getOption('formatted');
$formatted = $input->getOption('formatted');
$lineLength = (int) $input->getOption('line-length');

$schemaDumper = $this->getDependencyFactory()->getSchemaDumper();
Expand All @@ -100,6 +100,7 @@ public function execute(
$dirs = $configuration->getMigrationDirectories();
$namespace = key($dirs);
}
assert(is_string($namespace));

$path = $schemaDumper->dump(
$versionNumber,
Expand All @@ -111,6 +112,7 @@ public function execute(
$editorCommand = $input->getOption('editor-cmd');

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int

$path = is_string($path) ? $path : getcwd();

if (!is_string($path) || !is_dir($path) || !is_writable($path)) {
$output->writeln('<error>Directory not writeable!</error>');

return 1;
}

$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $direction, $sql);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
} elseif (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$fqcn = $namespace . '\\Version' . $versionNumber;
$path = $migrationGenerator->generateMigration($versionNumber, $namespace);

$editorCommand = $input->getOption('editor-cmd');

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
}

Expand Down
11 changes: 9 additions & 2 deletions lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$this->outputHeader($output);

$versionAlias = (string) $input->getArgument('version');
$versionAlias = $input->getArgument('version');
$path = $input->getOption('write-sql');

try {
Expand Down Expand Up @@ -152,6 +152,11 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
$sql = $migrator->migrate($plan, $migratorConfiguration);

$path = is_string($path) ? $path : getcwd();
if (!is_string($path) || !is_dir($path) || !is_writable($path)) {
$output->writeln('<error>Directory not writeable!</error>');

return 1;
}
$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $plan->getDirection(), $sql);

Expand Down Expand Up @@ -185,7 +190,9 @@ private function checkExecutedUnavailableMigrations(
foreach ($executedUnavailableMigrations->getItems() as $executedUnavailableMigration) {
$output->writeln(sprintf(
' <comment>>></comment> %s (<comment>%s</comment>)',
$executedUnavailableMigration->getExecutedAt() ? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s') : null,
$executedUnavailableMigration->getExecutedAt() !== null
? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s')
: null,
$executedUnavailableMigration->getVersion()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,14 @@ private function showUnavailableVersions(OutputInterface $output, ExecutedMigrat
foreach ($executedUnavailableMigrations->getItems() as $executedUnavailableMigration) {
$table->addRow([
(string) $executedUnavailableMigration->getVersion(),
$executedUnavailableMigration->getExecutedAt()
$executedUnavailableMigration->getExecutedAt() !== null
? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s')
: null,
]);
}
$table->render();
}

/**
* @param AvailableMigration[] $versions
*/
private function showVersions(
AvailableMigrationsList $availableMigrationsSet,
ExecutedMigrationsSet $executedMigrationsSet,
Expand Down
Loading

0 comments on commit 473d1bb

Please sign in to comment.