Test Failed
Push — feature-laravel-5.4 ( edfc13...468b78 )
by Kirill
03:20
created

GitHubDocsImport::getDocsCurrentState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
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\Article;
13
use App\Models\Docs;
14
use App\Services\GitHub\DocsStatus;
15
use App\Services\GitHub\ExternalDocsPage;
16
use App\Services\GitHub\GitHubConfigRepository;
17
use App\Services\GitHub\GitHubDocsManager;
18
use Illuminate\Console\Command;
19
use Illuminate\Support\Collection;
20
use Illuminate\Support\Str;
21
22
/**
23
 * Class GitHubDocsImport.
24
 */
25
class GitHubDocsImport extends Command
26
{
27
    /**
28
     * The name and signature of the console command.
29
     * @var string
30
     */
31
    protected $signature = 'docs:import';
32
33
    /**
34
     * The console command description.
35
     * @var string
36
     */
37
    protected $description = 'Import documentation from GitHub.';
38
39
    /**
40
     * Execute the console command.
41
     * @param GitHubDocsManager      $manager
42
     * @param GitHubConfigRepository $config
43
     * @return void
44
     * @throws \Github\Exception\ErrorException
45
     * @throws \Github\Exception\InvalidArgumentException
46
     * @throws \RuntimeException
47
     */
48
    public function handle(GitHubDocsManager $manager, GitHubConfigRepository $config): void
49
    {
50
        $exists = $this->getDocsCurrentState(...$config->values());
0 ignored issues
show
Bug introduced by
The call to getDocsCurrentState() misses some required arguments starting with $repo.
Loading history...
Documentation introduced by
$config->values() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
52
        $files = $manager->findFiles(...$config->values())
0 ignored issues
show
Bug introduced by
The call to findFiles() misses some required arguments starting with $repo.
Loading history...
Documentation introduced by
$config->values() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
            ->reject(function (DocsStatus $status) use ($exists) {
54
                return $exists->where('github_hash', $status->getHash())->first();
55
            })
56
            ->each(function (DocsStatus $status) use ($config, $manager) {
57
                $args = $config->values();
58
                $args[] = $status->getPath();
59
60
                /** @var ExternalDocsPage $page */
61
                $page = $manager->import(...$args);
0 ignored issues
show
Bug introduced by
The call to import() misses some required arguments starting with $repo.
Loading history...
Documentation introduced by
$args is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63
                $docs = $this->getDocsModel(...$args);
0 ignored issues
show
Bug introduced by
The call to getDocsModel() misses some required arguments starting with $repo.
Loading history...
Documentation introduced by
$args is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
65
                $docs->content_source = $page->getContent();
66
                $docs->github_hash = $status->getHash();
67
68
                if (!$docs->category_id) {
69
                    $docs->category_id = 0; // TODO
70
                }
71
72
                if (!$docs->title) {
73
                    $docs->title = Str::ucfirst(str_replace(
74
                        ['-', '_'],
75
                        ' ',
76
                        $page->getUrl()
77
                    ));
78
                }
79
80
                $docs->save();
81
            });
82
    }
83
84
    /**
85
     * @param string $org
86
     * @param string $repo
87
     * @param string $branch
88
     * @return Collection
89
     */
90
    private function getDocsCurrentState(string $org, string $repo, string $branch)
91
    {
92
        return Docs::query()
93
            ->where('github_org', $org)
94
            ->where('github_repo', $repo)
95
            ->where('github_branch', $branch)
96
            ->get(['github_hash']);
97
    }
98
99
    /**
100
     * @param string $org
101
     * @param string $repo
102
     * @param string $branch
103
     * @param string $file
104
     * @return \Illuminate\Database\Eloquent\Model|Docs
105
     */
106
    private function getDocsModel(string $org, string $repo, string $branch, string $file)
107
    {
108
        return Docs::query()
109
            ->firstOrNew([
110
                'github_org'    => $org,
111
                'github_repo'   => $repo,
112
                'github_branch' => $branch,
113
                'github_file'   => $file,
114
            ]);
115
    }
116
}
117