Passed
Pull Request — 2.x (#16)
by Maxim
14:24
created

MultipleFilesStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator\Migrations\Strategy;
6
7
use Cycle\Database\Schema\AbstractTable;
8
use Cycle\Migrations\Atomizer\Atomizer;
9
use Cycle\Migrations\Atomizer\Renderer;
10
use Cycle\Migrations\Atomizer\TableSorter;
0 ignored issues
show
Bug introduced by
The type Cycle\Migrations\Atomizer\TableSorter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Cycle\Migrations\Config\MigrationConfig;
12
use Cycle\Schema\Generator\Migrations\MigrationImage;
13
use Cycle\Schema\Generator\Migrations\NameGeneratorInterface;
14
15
final class MultipleFilesStrategy implements GeneratorStrategyInterface
16
{
17
    private static int $sec = 0;
18
19
    public function __construct(
20
        private readonly MigrationConfig $migrationConfig,
21
        private readonly NameGeneratorInterface $nameGenerator,
22
        private readonly TableSorter $tableSorter = new TableSorter()
23
    ) {
24
    }
25
26
    /**
27
     * @param non-empty-string $database
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
28
     * @param array<AbstractTable> $tables
29
     *
30
     * @return array<MigrationImage>
31
     */
32
    public function generate(string $database, array $tables): array
33
    {
34
        $atomizer = new Atomizer(new Renderer());
35
36
        $images = [];
37
        foreach ($this->tableSorter->sort($tables) as $table) {
38
            if ($table->getComparator()->hasChanges()) {
39
                $atomizer->setTables([$table]);
0 ignored issues
show
Bug introduced by
The method setTables() does not exist on Cycle\Migrations\Atomizer\Atomizer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
                $atomizer->/** @scrutinizer ignore-call */ 
40
                           setTables([$table]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
41
                $image = new MigrationImage($this->migrationConfig, $database);
42
                $image->setName($this->nameGenerator->generate($atomizer));
43
                $image->fileNamePattern = self::$sec++ . '_{database}_{name}';
44
45
                $atomizer->declareChanges($image->getClass()->getMethod('up'));
46
                $atomizer->revertChanges($image->getClass()->getMethod('down'));
47
48
                $images[] = $image;
49
            }
50
        }
51
52
        return $images;
53
    }
54
}
55