Completed
Push — master ( 3fdc21...2574b5 )
by ReliQ
01:51
created

Publish   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 25 42 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Console\Commands;
6
7
use Carbon\Carbon;
8
use Illuminate\Console\Command;
9
use ReliqArts\Docweaver\Contracts\Documentation\Publisher;
10
use ReliqArts\Docweaver\Exceptions\InvalidDirectory;
11
12
class Publish extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'docweaver:publish 
20
                            {product? : Product name}
21
                            {source? : Product repository}
22
                            {--y|y : Whether to skip confirmation}
23
                            ';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Publish documentation for product';
31
32
    /**
33
     * @var Publisher
34
     */
35
    protected $publisher;
36
37
    /**
38
     * Create a new command instance.
39
     *
40
     * @param Publisher $publisher
41
     */
42
    public function __construct(Publisher $publisher)
43
    {
44
        parent::__construct();
45
46
        $this->publisher = $publisher;
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @throws InvalidDirectory
53
     */
54
    public function handle(): void
55
    {
56
        $productName = $this->argument('product');
57
        $productSource = $this->argument('source');
58
        $skipConfirmation = $this->option('y');
59
        $confirmationMessage = sprintf(
60
            "This command will attempt to pull documentation for product (%s) from %s}. \n"
61
            . 'Please ensure your internet connection is stable. Ready?',
62
            $productName,
63
            $productSource
64
        );
65
66
        $this->comment(PHP_EOL
67
            . "<info>♣♣♣</info> Docweaver DocumentationPublisher \n"
68
            . 'Help is here, try: php artisan docweaver:publish --help');
69
70 View Code Duplication
        if ($skipConfirmation || $this->confirm($confirmationMessage)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $this->info("Publishing {$productName}.\nT: " . Carbon::now()->toCookieString() . "\n----------");
72
73
            $result = $this->publisher->publish($productName, $productSource, $this);
0 ignored issues
show
Bug introduced by
It seems like $productName defined by $this->argument('product') on line 56 can also be of type array or null; however, ReliqArts\Docweaver\Cont...on\Publisher::publish() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $productSource defined by $this->argument('source') on line 57 can also be of type array or null; however, ReliqArts\Docweaver\Cont...on\Publisher::publish() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
75
            if ($result->isSuccess()) {
76
                $this->info(PHP_EOL . '----------');
77
                $this->comment("<info>✔</info> Done. {$result->getMessage()}");
78
79
                // Display results
80
                $this->line('');
81
                $headers = ['Time', 'Versions', 'Published', 'Updated'];
82
                $data = $result->getData();
83
                $rows = [[
84
                    $data->executionTime,
85
                    count($data->versions),
86
                    count($data->versionsPublished),
87
                    count($data->versionsUpdated),
88
                ]];
89
                $this->table($headers, $rows);
90
                $this->line(PHP_EOL);
91
            } else {
92
                $this->line(PHP_EOL . "<error>✘</error> {$result->getError()}");
93
            }
94
        }
95
    }
96
}
97