Completed
Push — master ( fb9109...c0d92b )
by Mahmoud
03:27
created

GenerateApiDocumentationsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Documentation\UI\CLI\Commands;
4
5
use App\Containers\Documentation\Actions\GenerateAPIDocsAction;
6
use App\Containers\Documentation\Objects\PublicApi;
7
use App\Containers\Documentation\Objects\PrivateApi;
8
use App\Port\Console\Abstracts\ConsoleCommand;
9
10
/**
11
 * Class GenerateApiDocumentationsCommand
12
 *
13
 * @author  Mahmoud Zalt  <[email protected]>
14
 */
15
class GenerateApiDocumentationsCommand extends ConsoleCommand
16
{
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = "apidoc:generate";
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = "Generate API Documentations (using API Doc JS)";
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * @param \App\Containers\Documentation\Actions\GenerateAPIDocsAction $action
44
     */
45
    public function handle(GenerateAPIDocsAction $action)
46
    {
47
        // TODO: add optional argument array, allowing user to specify a type or more otherwise take all
48
49
        $types = [
50
            PrivateApi::TYPE,
51
            PublicApi::TYPE,
52
        ];
53
54
        echo "Generating API Documentations " . implode('& ', $types) . ".\n";
55
56
        foreach ($types as $type) {
57
            $documentationUrls[] = "[ {your-domain}/{$action->run($type)} ]";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$documentationUrls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $documentationUrls = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
58
        }
59
60
        echo "Done! You can access your API Docs at: " . implode(' & ', $documentationUrls) . ".\n";
0 ignored issues
show
Bug introduced by
The variable $documentationUrls does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
61
    }
62
}
63