GitHubDocsSync::handle()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 55
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 8
nop 1
dl 0
loc 55
rs 7.8235
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Console\Commands;
11
12
use App\Models\Docs;
13
use App\Models\DocsPage;
14
use Illuminate\Console\Command;
15
use Service\DocsImporter\DocsFileInterface;
16
use Service\DocsImporter\DocsImporterManager;
17
18
/**
19
 * Class GitHubDocsSync.
20
 */
21
class GitHubDocsSync extends Command
22
{
23
    /**
24
     * The name and signature of the console command.
25
     *
26
     * @var string
27
     */
28
    protected $signature = 'docs:sync {?--force}';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Sync documentation from GitHub.';
36
37
    /**
38
     * Execute the console command.
39
     * @param  DocsImporterManager $manager
40
     * @return void
41
     */
42
    public function handle(DocsImporterManager $manager): void
43
    {
44
        $isForce = $this->option('force');
45
        $docs = Docs::with('pages')->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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
        if ($docs->isEmpty()) {
48
            $this->info('Skipping: No docs provided.');
49
50
            return;
51
        }
52
53
        /** @var Docs $repo */
54
        foreach ($docs as $repo) {
55
            $importer = $manager->get($repo->importer);
56
            $config = $importer->config($repo->importer_config);
57
58
            foreach ($importer->files($config) as $file) {
59
                $prefix = '[' . $repo->title . ']::' . $file->getRoute();
60
61
                /*
62
                |--------------------------------------------------------------------------
63
                | Is file with required hash exists?
64
                |--------------------------------------------------------------------------
65
                */
66
                $updateRequired = $repo->pages->where('hash', $file->getHash())->isEmpty();
67
68
                if (! $updateRequired) {
69
                    $this->comment($prefix . ' has no available updates.');
70
71
                    if (! $isForce) {
72
                        continue;
73
                    } else {
74
                        $this->comment($prefix . ' will force update.');
75
                    }
76
                }
77
78
                /*
79
                |--------------------------------------------------------------------------
80
                | Is file with required identifier exists?
81
                |--------------------------------------------------------------------------
82
                */
83
                $page = $repo->pages->where('identify', $file->getId())->first();
84
85
                $message = $prefix . ' found. Updating sources...';
86
87
                if ($page === null) {
88
                    $message = $prefix . ' not exists. Uploading new...';
89
                    $page = new DocsPage(['identify' => $file->getId()]);
90
                }
91
92
                $this->comment($message);
93
                $this->updatePage($repo, $page, $file);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param Docs              $repo
100
     * @param DocsPage          $page
101
     * @param DocsFileInterface $file
102
     */
103
    private function updatePage(Docs $repo, DocsPage $page, DocsFileInterface $file)
104
    {
105
        $page->docs()->associate($repo);
106
107
        if ($page->category_id === null) {
108
            $page->category_id = 0;
109
        }
110
111
        $page->slug = $file->getRoute();
112
        $page->hash = $file->getHash();
113
        $page->title = $file->getTitle();
114
        $page->content_source = $file->getContent();
115
116
        $page->save();
117
    }
118
}
119