Skip to content

Commit 144d67b

Browse files
committed
Add code generation into the mix
1 parent 53bea8f commit 144d67b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+365
-139
lines changed

psalm-baseline.xml

+51-48
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.4.1@9fd7a7d885b3a216cff8dec9d8c21a132f275224">
2+
<files psalm-version="4.6.2@bca09d74adc704c4eaee36a3c3e9d379e290fc3b">
33
<file src="src/DataIntegrity.php">
44
<MissingParamType occurrences="1">
55
<code>$value</code>
66
</MissingParamType>
77
<MixedArgument occurrences="1">
88
<code>$column_default</code>
99
</MixedArgument>
10-
<MixedArrayOffset occurrences="5">
11-
<code>$existing_row[$field]</code>
12-
<code>$existing_row[$key]</code>
13-
<code>$new_row[$key]</code>
14-
<code>$new_row[$key]</code>
15-
<code>$new_row[$key]</code>
16-
</MixedArrayOffset>
17-
<MixedAssignment occurrences="4">
10+
<MixedAssignment occurrences="3">
1811
<code>$column_default</code>
19-
<code>$key</code>
2012
<code>$row[$column_name]</code>
2113
<code>$value</code>
2214
</MixedAssignment>
@@ -162,14 +154,6 @@
162154
</PossiblyNullArgument>
163155
</file>
164156
<file src="src/Parser/SQLLexer.php">
165-
<ArgumentTypeCoercion occurrences="1">
166-
<code>\PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_OFFSET_CAPTURE</code>
167-
</ArgumentTypeCoercion>
168-
<InvalidArgument occurrences="3">
169-
<code>$tokens</code>
170-
<code>$tokens</code>
171-
<code>$tokens</code>
172-
</InvalidArgument>
173157
<PossiblyNullArrayOffset occurrences="1">
174158
<code>$tokens</code>
175159
</PossiblyNullArrayOffset>
@@ -178,24 +162,6 @@
178162
<DocblockTypeContradiction occurrences="1">
179163
<code>$next === null</code>
180164
</DocblockTypeContradiction>
181-
<MissingPropertyType occurrences="1">
182-
<code>$cache</code>
183-
</MissingPropertyType>
184-
<MixedArgument occurrences="1">
185-
<code>self::$cache</code>
186-
</MixedArgument>
187-
<MixedArrayAccess occurrences="1">
188-
<code>self::$cache[$sql]</code>
189-
</MixedArrayAccess>
190-
<MixedArrayAssignment occurrences="1">
191-
<code>self::$cache[$sql]</code>
192-
</MixedArrayAssignment>
193-
<MixedInferredReturnType occurrences="1">
194-
<code>SelectQuery|InsertQuery|UpdateQuery|TruncateQuery|DeleteQuery|DropTableQuery|ShowTablesQuery</code>
195-
</MixedInferredReturnType>
196-
<MixedReturnStatement occurrences="1">
197-
<code>self::$cache[$sql]</code>
198-
</MixedReturnStatement>
199165
<MixedReturnTypeCoercion occurrences="2">
200166
<code>$out</code>
201167
<code>array&lt;int, Token&gt;</code>
@@ -206,11 +172,6 @@
206172
<code>$type</code>
207173
</ArgumentTypeCoercion>
208174
</file>
209-
<file src="src/Parser/TruncateParser.php">
210-
<DocblockTypeContradiction occurrences="1">
211-
<code>$token === null</code>
212-
</DocblockTypeContradiction>
213-
</file>
214175
<file src="src/Parser/UpdateParser.php">
215176
<DocblockTypeContradiction occurrences="1">
216177
<code>$token === null</code>
@@ -231,9 +192,7 @@
231192
<code>$stmt-&gt;values</code>
232193
<code>$stmt-&gt;values</code>
233194
</MixedArgumentTypeCoercion>
234-
<PossiblyNullArgument occurrences="5">
235-
<code>$conn-&gt;getDatabaseName()</code>
236-
<code>$conn-&gt;getDatabaseName()</code>
195+
<PossiblyNullArgument occurrences="3">
237196
<code>$stmt-&gt;decimals</code>
238197
<code>$stmt-&gt;length</code>
239198
<code>$stmt-&gt;length</code>
@@ -671,11 +630,20 @@
671630
<code>$values</code>
672631
</MissingConstructor>
673632
</file>
633+
<file src="src/Schema/Column/Blob.php">
634+
<MixedOperand occurrences="1">
635+
<code>$this-&gt;getDefault()</code>
636+
</MixedOperand>
637+
</file>
674638
<file src="src/Schema/Column/CharacterColumn.php">
675-
<PossiblyNullPropertyAssignmentValue occurrences="2">
676-
<code>$character_set</code>
677-
<code>$collation</code>
678-
</PossiblyNullPropertyAssignmentValue>
639+
<MixedOperand occurrences="1">
640+
<code>$this-&gt;getDefault()</code>
641+
</MixedOperand>
642+
</file>
643+
<file src="src/Schema/Column/DecimalPointColumn.php">
644+
<MixedOperand occurrences="1">
645+
<code>$this-&gt;getDefault()</code>
646+
</MixedOperand>
679647
</file>
680648
<file src="src/Schema/Column/Defaultable.php">
681649
<MissingParamType occurrences="1">
@@ -685,11 +653,36 @@
685653
<code>getDefault</code>
686654
</MissingReturnType>
687655
</file>
656+
<file src="src/Schema/Column/EmptyConstructorTrait.php">
657+
<MixedOperand occurrences="1">
658+
<code>$this-&gt;getDefault()</code>
659+
</MixedOperand>
660+
</file>
661+
<file src="src/Schema/Column/HasOptionsTrait.php">
662+
<MixedOperand occurrences="1">
663+
<code>$this-&gt;getDefault()</code>
664+
</MixedOperand>
665+
</file>
688666
<file src="src/Schema/Column/IntegerColumn.php">
689667
<MissingParamType occurrences="1">
690668
<code>$mysql_default</code>
691669
</MissingParamType>
692670
</file>
671+
<file src="src/Schema/Column/IntegerColumnTrait.php">
672+
<MixedOperand occurrences="1">
673+
<code>$this-&gt;getDefault()</code>
674+
</MixedOperand>
675+
</file>
676+
<file src="src/Schema/Column/LongBlob.php">
677+
<MixedOperand occurrences="1">
678+
<code>$this-&gt;getDefault()</code>
679+
</MixedOperand>
680+
</file>
681+
<file src="src/Schema/Column/MediumBlob.php">
682+
<MixedOperand occurrences="1">
683+
<code>$this-&gt;getDefault()</code>
684+
</MixedOperand>
685+
</file>
693686
<file src="src/Schema/Column/MySqlDefaultTrait.php">
694687
<MissingParamType occurrences="1">
695688
<code>$mysql_default</code>
@@ -700,6 +693,16 @@
700693
<code>$mysql_default</code>
701694
</MissingParamType>
702695
</file>
696+
<file src="src/Schema/Column/TextTrait.php">
697+
<MixedOperand occurrences="1">
698+
<code>$this-&gt;getDefault()</code>
699+
</MixedOperand>
700+
</file>
701+
<file src="src/Schema/Column/TinyBlob.php">
702+
<MixedOperand occurrences="1">
703+
<code>$this-&gt;getDefault()</code>
704+
</MixedOperand>
705+
</file>
703706
<file src="src/Schema/TableDefinition.php">
704707
<MixedPropertyTypeCoercion occurrences="2">
705708
<code>$autoIncrementOffsets</code>

src/DataIntegrity.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function ensureColumnsPresent(
6363
) {
6464
foreach ($table_definition->columns as $column_name => $column) {
6565
$php_type = $column->getPhpType();
66-
$column_nullable = $column->isNullable;
66+
$column_nullable = $column->isNullable();
6767

6868
$column_default = $column instanceof Schema\Column\Defaultable ? $column->getDefault() : null;
6969

@@ -151,7 +151,7 @@ public static function coerceValueToColumn(
151151
) {
152152
$php_type = $column->getPhpType();
153153

154-
if ($column->isNullable && $value === null) {
154+
if ($column->isNullable() && $value === null) {
155155
return null;
156156
}
157157

src/Parser/ShowParser.php

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public function parse()
5757
default:
5858
throw new ParserException("Parser error: expected SHOW TABLES");
5959
}
60-
6160
}
6261

6362
private function parseShowTables(): ShowTablesQuery

src/Processor/CreateProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function makeTableDefinition(
6767
foreach ($stmt->fields as $field) {
6868
$definition_columns[$field->name] = $column = self::getDefinitionColumn($field->type);
6969

70-
$column->isNullable = (bool) $field->type->null;
70+
$column->setNullable((bool) $field->type->null);
7171

7272
if ($field->auto_increment && $column instanceof Column\IntegerColumn) {
7373
$column->autoIncrement();

src/Processor/Expression/Evaluator.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ public static function combineColumnTypes(array $types) : Column
290290

291291
if ($type_0_null) {
292292
$type = clone $types[1];
293-
$type->isNullable = true;
293+
$type->setNullable(true);
294294
return $type;
295295
}
296296

297297
if ($type_1_null) {
298298
$type = clone $types[0];
299-
$type->isNullable = true;
299+
$type->setNullable(true);
300300
return $type;
301301
}
302302
}
@@ -311,7 +311,7 @@ public static function combineColumnTypes(array $types) : Column
311311
$non_null_types = [];
312312

313313
foreach ($types as $type) {
314-
if ($type->isNullable) {
314+
if ($type->isNullable()) {
315315
$is_nullable = true;
316316
}
317317

@@ -326,7 +326,7 @@ public static function combineColumnTypes(array $types) : Column
326326

327327
if (count($non_null_types) === 1) {
328328
$type = clone $non_null_types[0];
329-
$type->isNullable = true;
329+
$type->setNullable(true);
330330
return $type;
331331
}
332332

@@ -357,7 +357,7 @@ public static function combineColumnTypes(array $types) : Column
357357
}
358358

359359
if ($is_nullable) {
360-
$column->isNullable = true;
360+
$column->setNullable(true);
361361
}
362362

363363
return $column;

src/Processor/Expression/FunctionEvaluator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function getColumnSchema(
121121
$column = new Column\BigInt(false, 10);
122122
}
123123

124-
$column->isNullable = true;
124+
$column->setNullable(true);
125125
return $column;
126126

127127
case 'MOD':
@@ -148,7 +148,7 @@ public static function getColumnSchema(
148148
}
149149

150150
$if = clone $if;
151-
$if->isNullable = false;
151+
$if->setNullable(false);
152152

153153
if ($if->getPhpType() === 'string') {
154154
return $if;
@@ -209,8 +209,8 @@ public static function getColumnSchema(
209209

210210
$date = new Column\Date();
211211

212-
if ($arg->isNullable) {
213-
$date->isNullable = true;
212+
if ($arg->isNullable()) {
213+
$date->setNullable(true);
214214
}
215215

216216
return $date;

src/Processor/JoinProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function process(
6363
$parts = explode('.%.', $name);
6464
$null_placeholder[$right_table_name . '.%.' . end($parts)] = null;
6565
$column = clone $column;
66-
$column->isNullable = true;
66+
$column->setNullable(true);
6767
$right_result->columns[$name] = $column;
6868
}
6969

src/Processor/SelectProcessor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private static function getSelectSchema(
512512
$use_cache
513513
);
514514

515-
$columns[$expr->name]->isNullable = true;
515+
$columns[$expr->name]->setNullable(true);
516516
}
517517
}
518518
}
@@ -622,7 +622,7 @@ function ($col) {
622622
if (isset($subquery_result->columns[$column_name])
623623
&& (\get_class($subquery_result->columns[$column_name])
624624
!== \get_class($column)
625-
|| $subquery_result->columns[$column_name]->isNullable !== $column->isNullable)
625+
|| $subquery_result->columns[$column_name]->isNullable() !== $column->isNullable())
626626
) {
627627
$columns[$column_name] = Expression\Evaluator::combineColumnTypes([
628628
$subquery_result->columns[$column_name],

src/Processor/ShowIndexProcessor.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Vimeo\MysqlEngine\Processor;
55

6-
76
use Vimeo\MysqlEngine\FakePdoInterface;
87
use Vimeo\MysqlEngine\Query\ShowIndexQuery;
98
use Vimeo\MysqlEngine\Schema\Column;
@@ -59,7 +58,7 @@ public static function process(
5958
'Sub_part' => null,
6059
// because PACK_KEYS is not implemented
6160
'Packed' => null,
62-
'Null' => $table_definition->columns[$column]->isNullable ? 'YES' : '',
61+
'Null' => $table_definition->columns[$column]->isNullable() ? 'YES' : '',
6362
// because Index does not have $mode (in the CreateIndex)
6463
'Index_type' => null,
6564
// because DISABLE KEYS is not implemented
@@ -72,4 +71,4 @@ public static function process(
7271
$result = self::applyWhere($conn, $scope, $stmt->whereClause, new QueryResult($rows, $columns));
7372
return new QueryResult(array_merge($result->rows), $result->columns);
7473
}
75-
}
74+
}

src/Schema/Column.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,31 @@ abstract class Column
66
/**
77
* @var bool
88
*/
9-
public $isNullable = true;
9+
private $isNullable = true;
10+
11+
public function isNullable() : bool
12+
{
13+
return $this->isNullable;
14+
}
15+
16+
/**
17+
* @return static
18+
*/
19+
public function setNullable(bool $is_nullable)
20+
{
21+
$this->isNullable = $is_nullable;
22+
return $this;
23+
}
24+
25+
public function getNullablePhp() : string
26+
{
27+
return '->setNullable(' . ($this->isNullable() ? 'true' : 'false') . ')';
28+
}
1029

1130
/**
1231
* @return 'int'|'string'|'float'|'null'
1332
*/
1433
abstract public function getPhpType() : string;
34+
35+
abstract public function getPhpCode() : string;
1536
}

src/Schema/Column/Blob.php

+9
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ public function __construct()
99
{
1010
parent::__construct(65535, 'binary', '_bin');
1111
}
12+
13+
public function getPhpCode() : string
14+
{
15+
$default = $this->getDefault() !== null ? '\'' . $this->getDefault() . '\'' : 'null';
16+
17+
return '(new \\' . static::class . '())'
18+
. ($this->hasDefault() ? '->setDefault(' . $default . ')' : '')
19+
. $this->getNullablePhp();
20+
}
1221
}

src/Schema/Column/Char.php

-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,4 @@
66
class Char extends CharacterColumn implements StringColumn, Defaultable
77
{
88
use MySqlDefaultTrait;
9-
10-
public function __construct(int $max_string_length, ?string $character_set = null, ?string $collation = null)
11-
{
12-
parent::__construct($max_string_length, $character_set, $collation);
13-
}
149
}

0 commit comments

Comments
 (0)