SchemaController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createArgumentsFromRequest() 0 11 2
A show() 0 8 1
A convertOutputFile() 0 10 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: vernerd
5
 * Date: 2019-09-24
6
 * Time: 20:07.
7
 */
8
9
namespace DanielWerner\LaravelSchemaCrawler\Http\Controllers;
10
11
use Illuminate\Http\Request;
12
use Illuminate\Routing\Controller;
13
use Symfony\Component\Process\Process;
14
use DanielWerner\LaravelSchemaCrawler\Facades\SchemaCrawler;
15
use DanielWerner\LaravelSchemaCrawler\SchemaCrawlerArguments;
16
17
class SchemaController extends Controller
18
{
19
    /**
20
     * @param Request $request
21
     * @return SchemaCrawlerArguments
22
     */
23
    private function createArgumentsFromRequest(Request $request): SchemaCrawlerArguments
24
    {
25
        // If the requested output format is not html, first generate scdot, and convert it manually afterwards
26
        return new SchemaCrawlerArguments(
27
            $request->output_file,
28
            $request->output_format !== 'html' ? 'scdot' : $request->output_format,
0 ignored issues
show
Bug introduced by
The property output_format does not seem to exist. Did you mean format?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
29
            $request->connection,
30
            $request->info_level,
31
            $request->command
32
        );
33
    }
34
35
    /**
36
     * @param Request $request
37
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
38
     */
39
    public function show(Request $request)
40
    {
41
        $outputFormat = $request->output_format ?? config('laravel-schemacrawler.output_format');
0 ignored issues
show
Bug introduced by
The property output_format does not seem to exist. Did you mean format?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
        $file = SchemaCrawler::crawl($this->createArgumentsFromRequest($request));
43
        $file = $this->convertOutputFile($outputFormat, $file);
44
45
        return response()->file($file)->deleteFileAfterSend();
0 ignored issues
show
Bug introduced by
The method file does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
46
    }
47
48
    /**
49
     * @param string $outputFormat
50
     * @param string $file
51
     * @return string
52
     */
53
    private function convertOutputFile(string $outputFormat, string $file): string
54
    {
55
        // Workaround, the schemacrawler cannot call the dot, when called from php using Valet.
56
        // It is necessary to generate the schema in scdot format, and manually convert the to the output format
57
        // @see: https://github.com/schemacrawler/SchemaCrawler/issues/179
58
        $process = new Process(['dot', '-T', $outputFormat, $file, '-o', $file]);
59
        $process->run();
60
61
        return $file;
62
    }
63
}
64