Issues (15)

src/Operation/Column/Column.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\Column;
6
7
use Cycle\Database\Schema\AbstractColumn;
0 ignored issues
show
The type Cycle\Database\Schema\AbstractColumn 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...
8
use Cycle\Database\Schema\AbstractTable;
9
use Cycle\Migrations\Exception\Operation\ColumnException;
10
use Cycle\Migrations\Operation\AbstractOperation;
11
use Cycle\Migrations\Operation\Traits\OptionsTrait;
12
13
abstract class Column extends AbstractOperation
14
{
15
    use OptionsTrait;
16
17
    /**
18
     * Some options has set of aliases.
19
     */
20
    protected array $aliases = [
21
        'size' => ['length', 'limit'],
22
        'default' => ['defaultValue'],
23
        'null' => ['nullable'],
24
    ];
25
26 288
    public function __construct(
27
        string $table,
28
        protected string $name,
29
        protected string $type = 'string',
30
        array $options = []
31
    ) {
32 288
        $this->options = $options;
33 288
        parent::__construct($table);
34
    }
35
36
    /**
37
     * @throws ColumnException
38
     */
39 264
    protected function declareColumn(AbstractTable $schema): AbstractColumn
40
    {
41 264
        $column = $schema->column($this->name);
42
43
        //Type configuring
44 264
        if (method_exists($column, $this->type)) {
45 90
            $arguments = [];
46
            $variadic = false;
47 90
48 90
            $method = new \ReflectionMethod($column, $this->type);
49 40
            foreach ($method->getParameters() as $parameter) {
50 22
                if ($this->hasOption($parameter->getName())) {
51 18
                    $arguments[$parameter->getName()] = $this->getOption($parameter->getName());
52 8
                } elseif (!$parameter->isOptional()) {
53 8
                    throw new ColumnException(
54
                        "Option '{$parameter->getName()}' are required to define column with type '{$this->type}'"
55
                    );
56 10
                } elseif ($parameter->isDefaultValueAvailable()) {
57
                    $arguments[$parameter->getName()] = $parameter->getDefaultValue();
58
                } elseif ($parameter->isVariadic()) {
59
                    $variadic = true;
60 84
                }
61
            }
62 246
63
            \call_user_func_array(
64
                [$column, $this->type],
65 264
                $variadic ? $arguments + $this->options + $column->getAttributes() : $arguments,
66
            );
67 264
        } else {
68 232
            $column->type($this->type);
69
        }
70
71 264
        $column->nullable($this->getOption('nullable', false));
72
73
        if ($this->hasOption('default')) {
74
            $column->defaultValue($this->getOption('default', null));
75
        }
76
77
        return $column;
78
    }
79
}
80