GitHubDocsSync::updatePage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
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