Skip to content

Commit 6579c03

Browse files
barryvdhlaravel-ide-helperhafezdivandari
authored
Remove doctrine (#1512)
* Remove doctrine * Fix tests * normalize composer.json * Add back platform * Bump integration versions * composer fix-style * Fix stability * Bump minimum to 10.36 * Bump minimum to 10.38 * More types * Fast fail off * Bump docblock * composer fix-style * add bpchar * Use match for type detection Co-authored-by: Hafez Divandari <[email protected]> * Use driverName Co-authored-by: Hafez Divandari <[email protected]> * fix match * TWeak types * Update snapshots * Remove testbench 7 * composer fix-style * Update src/Console/ModelsCommand.php Co-authored-by: Hafez Divandari <[email protected]> * Update src/Console/ModelsCommand.php Co-authored-by: Hafez Divandari <[email protected]> * Tweak bool * Update src/Console/ModelsCommand.php Co-authored-by: Hafez Divandari <[email protected]> * composer fix-style * Remove custom types, default to string --------- Co-authored-by: barryvdh <[email protected]> Co-authored-by: laravel-ide-helper <[email protected]> Co-authored-by: Hafez Divandari <[email protected]>
1 parent ed39977 commit 6579c03

File tree

22 files changed

+74
-187
lines changed

22 files changed

+74
-187
lines changed

.github/workflows/fix-code-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup PHP
1919
uses: shivammathur/setup-php@v2
2020
with:
21-
php-version: 8.0
21+
php-version: 8.3
2222
coverage: none
2323

2424
- name: Install dependencies

.github/workflows/run-integration-tests.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ jobs:
1919
strategy:
2020
fail-fast: false
2121
matrix:
22-
php: [8.3, 8.2, 8.1, 8.0]
23-
laravel: [10.*, 9.*]
24-
exclude:
25-
- php: 8.0
26-
laravel: 10.*
27-
- php: 8.3
28-
laravel: 9.*
22+
php: [8.3, 8.2, 8.1]
23+
laravel: [10.*]
24+
2925
name: P${{ matrix.php }} - Laravel${{ matrix.laravel }}
3026
steps:
3127
- name: Checkout code

.github/workflows/run-static-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v4
16-
16+
1717
- name: Setup PHP
1818
uses: shivammathur/setup-php@v2
1919
with:
20-
php-version: 8.1
20+
php-version: 8.3
2121
coverage: none
2222
extensions: pdo_sqlite
2323

.github/workflows/run-tests.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ jobs:
1818
COMPOSER_NO_INTERACTION: 1
1919

2020
strategy:
21+
fail-fast: false
2122
matrix:
2223
os: [ubuntu-20.04, windows-2019]
23-
php: [8.3, 8.2, 8.1, 8.0]
24-
laravel: [9.*, 10.*]
25-
dependency-version: [prefer-lowest, prefer-stable]
26-
exclude:
27-
- php: 8.0
28-
laravel: 10.*
29-
- php: 8.0
30-
dependency-version: prefer-lowest
24+
php: [8.3, 8.2, 8.1]
25+
laravel: [10.*]
26+
stability: [prefer-lowest, prefer-stable]
27+
3128
steps:
3229
- name: Set git to use LF
3330
if: ${{ matrix.os == 'windows-2019' }}
@@ -50,7 +47,7 @@ jobs:
5047
composer remove vimeo/psalm --no-update --dev
5148
composer remove friendsofphp/php-cs-fixer --no-update --dev
5249
composer require "laravel/framework:${{ matrix.laravel }}" --no-update --no-progress
53-
composer update --prefer-dist --no-progress
50+
composer update --prefer-dist --no-progress --${{ matrix.stability }}
5451
5552
- name: Execute Unit Tests
5653
run: composer test-ci

README.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,6 @@ add support for creating a new dedicated class instead of using local scopes in
271271

272272
If for some reason it's undesired to have them generated (one for each column), you can disable this via config `write_model_external_builder_methods` and setting it to `false`.
273273

274-
#### Unsupported or custom database types
275-
276-
Common column types (e.g. varchar, integer) are correctly mapped to PHP types (`string`, `int`).
277-
278-
But sometimes you may want to use custom column types in your database like `geography`, `jsonb`, `citext`, `bit`, etc. which may throw an "Unknown database type"-Exception.
279-
280-
For those special cases, you can map them via the config `custom_db_types`. Example:
281-
```php
282-
'custom_db_types' => [
283-
'mysql' => [
284-
'geography' => 'array',
285-
'point' => 'array',
286-
],
287-
'postgresql' => [
288-
'jsonb' => 'string',
289-
'_int4' => 'array',
290-
],
291-
],
292-
```
293-
294274
#### Custom Relationship Types
295275

296276
If you are using relationships not built into Laravel you will need to specify the name and returning class in the config to get proper generation.

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
"require": {
2323
"php": "^8.0",
2424
"ext-json": "*",
25-
"barryvdh/reflection-docblock": "^2.0.6",
25+
"barryvdh/reflection-docblock": "^2.1.1",
2626
"composer/class-map-generator": "^1.0",
27-
"doctrine/dbal": "^2.6 || ^3.1.4",
28-
"illuminate/console": "^9 || ^10",
29-
"illuminate/filesystem": "^9 || ^10",
30-
"illuminate/support": "^9 || ^10",
27+
"illuminate/console": "^10",
28+
"illuminate/database": "^10.38",
29+
"illuminate/filesystem": "^10",
30+
"illuminate/support": "^10",
3131
"nikic/php-parser": "^4.18 || ^5",
3232
"phpdocumentor/type-resolver": "^1.1.0"
3333
},
@@ -37,7 +37,7 @@
3737
"illuminate/config": "^9 || ^10",
3838
"illuminate/view": "^9 || ^10",
3939
"mockery/mockery": "^1.4",
40-
"orchestra/testbench": "^7 || ^8",
40+
"orchestra/testbench": "^8",
4141
"phpunit/phpunit": "^9",
4242
"spatie/phpunit-snapshot-assertions": "^4",
4343
"vimeo/psalm": "^5.4"

config/ide-helper.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,36 +200,6 @@
200200

201201
],
202202

203-
/*
204-
|--------------------------------------------------------------------------
205-
| Support for custom DB types
206-
|--------------------------------------------------------------------------
207-
|
208-
| This setting allow you to map any custom database type (that you may have
209-
| created using CREATE TYPE statement or imported using database plugin
210-
| / extension to a Doctrine type.
211-
|
212-
| Each key in this array is a name of the Doctrine2 DBAL Platform. Currently valid names are:
213-
| 'postgresql', 'db2', 'drizzle', 'mysql', 'oracle', 'sqlanywhere', 'sqlite', 'mssql'
214-
|
215-
| This name is returned by getName() method of the specific Doctrine/DBAL/Platforms/AbstractPlatform descendant
216-
|
217-
| The value of the array is an array of type mappings. Key is the name of the custom type,
218-
| (for example, "jsonb" from Postgres 9.4) and the value is the name of the corresponding Doctrine2 type (in
219-
| our case it is 'json_array'. Doctrine types are listed here:
220-
| https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#types
221-
|
222-
| So to support jsonb in your models when working with Postgres, just add the following entry to the array below:
223-
|
224-
| "postgresql" => array(
225-
| "jsonb" => "json_array",
226-
| ),
227-
|
228-
*/
229-
'custom_db_types' => [
230-
231-
],
232-
233203
/*
234204
|--------------------------------------------------------------------------
235205
| Support for camel cased models

src/Console/ModelsCommand.php

Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
1818
use Barryvdh\Reflection\DocBlock\Tag;
1919
use Composer\ClassMapGenerator\ClassMapGenerator;
20-
use Doctrine\DBAL\Exception as DBALException;
21-
use Doctrine\DBAL\Types\Type;
2220
use Illuminate\Console\Command;
2321
use Illuminate\Contracts\Database\Eloquent\Castable;
2422
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
@@ -39,6 +37,7 @@
3937
use Illuminate\Database\Eloquent\Relations\MorphTo;
4038
use Illuminate\Database\Eloquent\Relations\MorphToMany;
4139
use Illuminate\Database\Eloquent\Relations\Relation;
40+
use Illuminate\Database\Schema\Builder;
4241
use Illuminate\Filesystem\Filesystem;
4342
use Illuminate\Support\Arr;
4443
use Illuminate\Support\Collection;
@@ -244,8 +243,6 @@ protected function generateDocs($loadModels, $ignore = '')
244243
*/
245244
\n\n";
246245

247-
$hasDoctrine = interface_exists('Doctrine\DBAL\Driver');
248-
249246
if (empty($loadModels)) {
250247
$models = $this->loadModels();
251248
} else {
@@ -288,9 +285,7 @@ protected function generateDocs($loadModels, $ignore = '')
288285

289286
$model = $this->laravel->make($name);
290287

291-
if ($hasDoctrine) {
292-
$this->getPropertiesFromTable($model);
293-
}
288+
$this->getPropertiesFromTable($model);
294289

295290
if (method_exists($model, 'getCasts')) {
296291
$this->castPropertiesType($model);
@@ -314,13 +309,6 @@ protected function generateDocs($loadModels, $ignore = '')
314309
}
315310
}
316311

317-
if (!$hasDoctrine) {
318-
$this->error(
319-
'Warning: `"doctrine/dbal": "~2.3"` is required to load database information. ' .
320-
'Please require that in your composer.json and run `composer update`.'
321-
);
322-
}
323-
324312
return $output;
325313
}
326314

@@ -508,95 +496,52 @@ protected function getTypeOverride($type)
508496
*
509497
* @param Model $model
510498
*
511-
* @throws DBALException If custom field failed to register
512499
*/
513500
public function getPropertiesFromTable($model)
514501
{
515-
$database = $model->getConnection()->getDatabaseName();
516-
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
517-
$schema = $model->getConnection()->getDoctrineSchemaManager();
518-
$databasePlatform = $schema->getDatabasePlatform();
519-
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
520-
521-
if (strpos($table, '.')) {
522-
[$database, $table] = explode('.', $table);
523-
}
524-
525-
$platformName = $databasePlatform->getName();
526-
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
527-
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
528-
try {
529-
if (!Type::hasType($yourTypeName)) {
530-
Type::addType($yourTypeName, get_class(Type::getType($doctrineTypeName)));
531-
}
532-
} catch (DBALException $exception) {
533-
$this->error("Failed registering custom db type \"$yourTypeName\" as \"$doctrineTypeName\"");
534-
throw $exception;
535-
}
536-
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
537-
}
502+
$table = $model->getTable();
503+
$schema = $model->getConnection()->getSchemaBuilder();
504+
$columns = $schema->getColumns($table);
505+
$driverName = $model->getConnection()->getDriverName();
538506

539-
$columns = $schema->listTableColumns($table, $database);
540507

541508
if (!$columns) {
542509
return;
543510
}
544511

545512
$this->setForeignKeys($schema, $table);
546513
foreach ($columns as $column) {
547-
$name = $column->getName();
514+
$name = $column['name'];
548515
if (in_array($name, $model->getDates())) {
549516
$type = $this->dateClass;
550517
} else {
551-
$type = $column->getType()->getName();
552-
switch ($type) {
553-
case 'string':
554-
case 'text':
555-
case 'date':
556-
case 'time':
557-
case 'guid':
558-
case 'datetimetz':
559-
case 'datetime':
560-
case 'decimal':
561-
case 'binary':
562-
$type = 'string';
563-
break;
564-
case 'integer':
565-
case 'bigint':
566-
case 'smallint':
567-
$type = 'integer';
568-
break;
569-
case 'boolean':
570-
switch ($platformName) {
571-
case 'sqlite':
572-
case 'mysql':
573-
$type = 'integer';
574-
break;
575-
default:
576-
$type = 'boolean';
577-
break;
578-
}
579-
break;
580-
case 'float':
581-
$type = 'float';
582-
break;
583-
default:
584-
$type = 'mixed';
585-
break;
586-
}
518+
// Match types to php equivalent
519+
$type = match ($column['type_name']) {
520+
'tinyint', 'bit',
521+
'integer', 'int', 'int4',
522+
'smallint', 'int2',
523+
'mediumint',
524+
'bigint', 'int8' => 'integer',
525+
526+
'boolean', 'bool' => 'boolean',
527+
528+
'float', 'real', 'float4',
529+
'double', 'float8' => 'float',
530+
531+
default => 'string',
532+
};
587533
}
588534

589-
$comment = $column->getComment();
590-
if (!$column->getNotnull()) {
535+
if ($column['nullable']) {
591536
$this->nullableColumns[$name] = true;
592537
}
593538
$this->setProperty(
594539
$name,
595540
$this->getTypeInModel($model, $type),
596541
true,
597542
true,
598-
$comment,
599-
!$column->getNotnull()
543+
$column['comment'],
544+
$column['nullable']
600545
);
601546
if ($this->write_model_magic_where) {
602547
$builderClass = $this->write_model_external_builder_methods
@@ -1681,14 +1626,13 @@ protected function runModelHooks($model): void
16811626
}
16821627

16831628
/**
1684-
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
1629+
* @param Builder $schema
16851630
* @param string $table
1686-
* @throws DBALException
16871631
*/
16881632
protected function setForeignKeys($schema, $table)
16891633
{
1690-
foreach ($schema->listTableForeignKeys($table) as $foreignKeyConstraint) {
1691-
foreach ($foreignKeyConstraint->getLocalColumns() as $columnName) {
1634+
foreach ($schema->getForeignKeys($table) as $foreignKeyConstraint) {
1635+
foreach ($foreignKeyConstraint['columns'] as $columnName) {
16921636
$this->foreignKeyConstraintsColumns[] = $columnName;
16931637
}
16941638
}

tests/Console/ModelsCommand/AllowGlobDirectory/__snapshots__/Test__test__1.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
* @property string $timestamptz_not_nullable
7373
* @property integer|null $year_nullable
7474
* @property integer $year_not_nullable
75-
* @property mixed|null $binary_nullable
76-
* @property mixed $binary_not_nullable
75+
* @property string|null $binary_nullable
76+
* @property string $binary_not_nullable
7777
* @property string|null $uuid_nullable
7878
* @property string $uuid_not_nullable
7979
* @property string|null $ipaddress_nullable

tests/Console/ModelsCommand/DoesNotGeneratePhpdocWithExternalEloquentBuilder/__snapshots__/Test__test__1.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
* @property string $timestamptz_not_nullable
7979
* @property integer|null $year_nullable
8080
* @property integer $year_not_nullable
81-
* @property mixed|null $binary_nullable
82-
* @property mixed $binary_not_nullable
81+
* @property string|null $binary_nullable
82+
* @property string $binary_not_nullable
8383
* @property string|null $uuid_nullable
8484
* @property string $uuid_not_nullable
8585
* @property string|null $ipaddress_nullable

0 commit comments

Comments
 (0)