Skip to content

Commit b9ac034

Browse files
Fix empty string default not being parsed correctly (#70)
* Fix empty string default value not being correctly parsed * CS fix
1 parent 40c7acd commit b9ac034

File tree

12 files changed

+51
-36
lines changed

12 files changed

+51
-36
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"post-autoload-dump": [
3636
"@php ./vendor/bin/testbench package:discover --ansi"
3737
],
38-
"test": ["vendor/bin/phpunit"]
38+
"test": ["vendor/bin/phpunit"],
39+
"lint": "vendor/bin/php-cs-fixer fix"
3940
},
4041
"extra": {
4142
"laravel": {

src/Definitions/ColumnDefinition.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getComment(): ?string
123123
{
124124
return $this->comment;
125125
}
126-
126+
127127
/**
128128
* @return string|null
129129
*/
@@ -411,7 +411,7 @@ public function setIsUUID(bool $isUUID): ColumnDefinition
411411

412412
protected function isNullableMethod($methodName)
413413
{
414-
return ! in_array($methodName, ['softDeletes', 'morphs', 'nullableMorphs', 'rememberToken', 'nullableUuidMorphs']) && !$this->isPrimaryKeyMethod($methodName);
414+
return ! in_array($methodName, ['softDeletes', 'morphs', 'nullableMorphs', 'rememberToken', 'nullableUuidMorphs']) && ! $this->isPrimaryKeyMethod($methodName);
415415
}
416416

417417
protected function isPrimaryKeyMethod($methodName)
@@ -514,8 +514,8 @@ public function render(): string
514514
$this->nullable = true;
515515
}
516516

517-
if($this->isNullableMethod($finalMethodName)){
518-
if($this->nullable === true){
517+
if ($this->isNullableMethod($finalMethodName)) {
518+
if ($this->nullable === true) {
519519
$initialString .= '->nullable()';
520520
}
521521
}

src/GeneratorManagers/BaseGeneratorManager.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace LaravelMigrationGenerator\GeneratorManagers;
44

5-
use LaravelMigrationGenerator\Definitions\ViewDefinition;
6-
use LaravelMigrationGenerator\Helpers\Dependency;
75
use LaravelMigrationGenerator\Helpers\ConfigResolver;
6+
use LaravelMigrationGenerator\Definitions\ViewDefinition;
87
use LaravelMigrationGenerator\Helpers\DependencyResolver;
98
use LaravelMigrationGenerator\Definitions\TableDefinition;
109
use LaravelMigrationGenerator\GeneratorManagers\Interfaces\GeneratorManagerInterface;
11-
use LaravelMigrationGenerator\Helpers\DependencyResolverV2;
12-
use LaravelMigrationGenerator\Helpers\DependencyResolverV3;
1310

1411
abstract class BaseGeneratorManager implements GeneratorManagerInterface
1512
{

src/GeneratorManagers/Interfaces/GeneratorManagerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace LaravelMigrationGenerator\GeneratorManagers\Interfaces;
44

5-
use LaravelMigrationGenerator\Definitions\TableDefinition;
65
use LaravelMigrationGenerator\Definitions\ViewDefinition;
6+
use LaravelMigrationGenerator\Definitions\TableDefinition;
77

88
interface GeneratorManagerInterface
99
{

src/Generators/Concerns/CleansUpMorphColumns.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Support\Str;
66
use LaravelMigrationGenerator\Definitions\ColumnDefinition;
77
use LaravelMigrationGenerator\Generators\BaseTableGenerator;
8-
use LaravelMigrationGenerator\Tokenizers\Interfaces\ColumnTokenizerInterface;
98

109
/**
1110
* Trait CleansUpMorphColumns

src/Helpers/DependencyResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function build()
6363
foreach ($indicesForStart as $index) {
6464
$startDefinition->removeIndexDefinition($index);
6565
}
66-
if(!in_array($start, $sorted)) {
66+
if (! in_array($start, $sorted)) {
6767
$definitions[] = $startDefinition;
6868
}
6969

@@ -76,7 +76,7 @@ protected function build()
7676
foreach ($indicesForEnd as $index) {
7777
$endDefinition->removeIndexDefinition($index);
7878
}
79-
if(!in_array($end, $sorted)) {
79+
if (! in_array($end, $sorted)) {
8080
$definitions[] = $endDefinition;
8181
}
8282

src/Helpers/ValueToString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static function parseCastedValue($value)
2626
if (Str::startsWith($value, 'float$:')) {
2727
return str_replace('float$:', '', $value);
2828
}
29-
if(Str::startsWith($value, 'binary$:')) {
30-
return 'b\''.str_replace('binary$:', '', $value).'\'';
29+
if (Str::startsWith($value, 'binary$:')) {
30+
return 'b\'' . str_replace('binary$:', '', $value) . '\'';
3131
}
3232

3333
return $value;

src/Tokenizers/BaseTokenizer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ public function __construct(string $value)
1616
$this->value = $value;
1717
$prune = false;
1818
$pruneSingleQuotes = false;
19-
//\(?\'(.+?)?\s(.+?)?\'\)?
2019

2120
//first get rid of any single quoted stuff with '' around it
22-
if(preg_match_all('/\'\'(.+?)\'\'/', $value, $matches)){
23-
foreach($matches[0] as $key => $singleQuoted){
21+
if (preg_match_all('/\'\'(.+?)\'\'/', $value, $matches)) {
22+
foreach ($matches[0] as $key => $singleQuoted) {
2423
$toReplace = $singleQuoted;
25-
$value = str_replace($toReplace, self::SINGLE_QUOTE_REPLACER.$matches[1][$key].self::SINGLE_QUOTE_REPLACER, $value);
24+
$value = str_replace($toReplace, self::SINGLE_QUOTE_REPLACER . $matches[1][$key] . self::SINGLE_QUOTE_REPLACER, $value);
2625
$pruneSingleQuotes = true;
2726
}
2827
}
28+
if (preg_match('/\'\'/', $value)) {
29+
$value = str_replace('\'\'', '$$EMPTY_STRING', $value);
30+
}
2931

3032
if (preg_match_all("/'(.+?)'/", $value, $matches)) {
3133
foreach ($matches[0] as $quoteWithSpace) {
@@ -36,6 +38,7 @@ public function __construct(string $value)
3638
$prune = true;
3739
}
3840
}
41+
$value = str_replace('$$EMPTY_STRING', '\'\'', $value);
3942
$this->tokens = array_map(function ($item) {
4043
return trim($item, ', ');
4144
}, str_getcsv($value, ' ', "'"));
@@ -45,7 +48,7 @@ public function __construct(string $value)
4548
return str_replace(self::SPACE_REPLACER, ' ', $item);
4649
}, $this->tokens);
4750
}
48-
if($pruneSingleQuotes){
51+
if ($pruneSingleQuotes) {
4952
$this->tokens = array_map(function ($item) {
5053
return str_replace(self::SINGLE_QUOTE_REPLACER, '\'', $item);
5154
}, $this->tokens);

src/Tokenizers/MySQL/ColumnTokenizer.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ protected function consumeNullable()
112112
$this->putBack($piece);
113113
}
114114

115-
if(Str::contains($this->columnDataType, 'text')){
115+
if (Str::contains($this->columnDataType, 'text')) {
116116
//text column types are explicitly nullable unless set to NOT NULL
117-
if($this->definition->isNullable() === null){
117+
if ($this->definition->isNullable() === null) {
118118
$this->definition->setNullable(true);
119119
}
120120
}
@@ -134,21 +134,21 @@ protected function consumeDefaultValue()
134134
$this->definition
135135
->setDefaultValue(null)
136136
->setUseCurrent(true);
137-
} elseif(preg_match("/b'([01]+)'/i", $this->definition->getDefaultValue(), $matches)){
137+
} elseif (preg_match("/b'([01]+)'/i", $this->definition->getDefaultValue(), $matches)) {
138138
// Binary digit, so let's convert to PHP's version
139139
$this->definition->setDefaultValue(ValueToString::castBinary($matches[1]));
140140
}
141141
if ($this->definition->getDefaultValue() !== null) {
142142
if ($this->isNumberType()) {
143143
if (Str::contains(strtoupper($this->columnDataType), 'INT')) {
144-
$this->definition->setDefaultValue((int)$this->definition->getDefaultValue());
144+
$this->definition->setDefaultValue((int) $this->definition->getDefaultValue());
145145
} else {
146146
//floats get converted to strings improperly, gotta do a string cast
147147
$this->definition->setDefaultValue(ValueToString::castFloat($this->definition->getDefaultValue()));
148148
}
149149
} else {
150-
if(!$this->isBinaryType()) {
151-
$this->definition->setDefaultValue((string)$this->definition->getDefaultValue());
150+
if (! $this->isBinaryType()) {
151+
$this->definition->setDefaultValue((string) $this->definition->getDefaultValue());
152152
}
153153
}
154154
}
@@ -164,10 +164,10 @@ protected function consumeComment()
164164
// next piece is the comment content
165165
$this->definition->setComment($this->consume());
166166
} else {
167-
$this->putBack($piece);
167+
$this->putBack($piece);
168168
}
169169
}
170-
170+
171171
protected function consumeCharacterSet()
172172
{
173173
$piece = $this->consume();
@@ -370,7 +370,8 @@ protected function isArrayType()
370370
return Str::contains($this->columnDataType, ['enum', 'set']);
371371
}
372372

373-
protected function isBinaryType(){
373+
protected function isBinaryType()
374+
{
374375
return Str::contains($this->columnDataType, ['bit']);
375376
}
376377

src/Tokenizers/MySQL/IndexTokenizer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ private function consumeForeignKey()
5959
$columns = [];
6060
$token = $this->consume();
6161

62-
while(!is_null($token)) {
62+
while (! is_null($token)) {
6363
$columns = array_merge($columns, $this->columnsToArray($token));
6464
$token = $this->consume();
65-
if(strtoupper($token) === 'REFERENCES') {
65+
if (strtoupper($token) === 'REFERENCES') {
6666
$this->putBack($token);
67+
6768
break;
6869
}
6970
}
@@ -76,11 +77,12 @@ private function consumeForeignKey()
7677

7778
$referencedColumns = [];
7879
$token = $this->consume();
79-
while(!is_null($token)){
80+
while (! is_null($token)) {
8081
$referencedColumns = array_merge($referencedColumns, $this->columnsToArray($token));
8182
$token = $this->consume();
82-
if(strtoupper($token) === 'ON'){
83+
if (strtoupper($token) === 'ON') {
8384
$this->putBack($token);
85+
8486
break;
8587
}
8688
}

0 commit comments

Comments
 (0)