Completed
Push — master ( b08809...5efe48 )
by Abdelrahman
01:22 queued 10s
created

CreateFormResponsesTable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 17 1
A down() 0 4 1
A jsonable() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Facades\Schema;
6
use Illuminate\Database\Schema\Blueprint;
7
use Illuminate\Database\Migrations\Migration;
8
9
class CreateFormResponsesTable extends Migration
10
{
11
    /**
12
     * Run the migrations.
13
     *
14
     * @return void
15
     */
16
    public function up()
17
    {
18
        Schema::create(config('rinvex.forms.tables.form_responses'), function (Blueprint $table) {
19
            // Columns
20
            $table->increments('id');
21
            $table->string('unique_identifier')->nullable();
22
            $table->{$this->jsonable()}('content');
23
            $table->integer('form_id')->unsigned();
24
            $table->nullableMorphs('user');
25
            $table->timestamps();
26
            $table->softDeletes();
27
28
            // Indexes
29
            $table->foreign('form_id')->references('id')->on(config('rinvex.forms.tables.forms'))
0 ignored issues
show
Bug introduced by
The method references does only exist in Illuminate\Database\Schema\ForeignKeyDefinition, but not in Illuminate\Support\Fluent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
30
                  ->onDelete('cascade')->onUpdate('cascade');
31
        });
32
    }
33
34
    /**
35
     * Reverse the migrations.
36
     *
37
     * @return void
38
     */
39
    public function down()
40
    {
41
        Schema::dropIfExists(config('rinvex.forms.tables.form_responses'));
42
    }
43
44
    /**
45
     * Get jsonable column data type.
46
     *
47
     * @return string
48
     */
49
    protected function jsonable(): string
50
    {
51
        $driverName = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
52
        $dbVersion = DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
53
        $isOldVersion = version_compare($dbVersion, '5.7.8', 'lt');
54
55
        return $driverName === 'mysql' && $isOldVersion ? 'text' : 'json';
56
    }
57
}
58