Test Failed
Push — feature-laravel-5.4 ( 43bafb...e7f3fd )
by Kirill
42:21
created

GitHubDocsSync::updatePage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 14
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
        $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...
45
46
        if ($docs->isEmpty()) {
47
            $this->info('Skipping: No docs provided.');
48
            return;
49
        }
50
51
        /** @var Docs $repo */
52
        foreach ($docs as $repo) {
53
            $importer = $manager->get($repo->importer);
54
            $config   = $importer->config($repo->organisation, $repo->repository, $repo->branch);
55
56
57
            foreach ($importer->files($config) as $file) {
58
                $prefix = '[' . $repo->title . ']::' . $file->getRoute();
59
60
                /*
61
                |--------------------------------------------------------------------------
62
                | Is file with required hash exists?
63
                |--------------------------------------------------------------------------
64
                */
65
                $updateRequired = $repo->pages->where('hash', $file->getHash())->isEmpty();
66
67
                if (!$updateRequired) {
68
                    $this->comment($prefix . ' has no available updates.');
69
                    continue;
70
                }
71
72
                /*
73
                |--------------------------------------------------------------------------
74
                | Is file with required identifier exists?
75
                |--------------------------------------------------------------------------
76
                */
77
                $page = $repo->pages->where('identify', $file->getId())->first();
78
79
80
                $message = $prefix . ' found. Updating sources...';
81
82
                if ($page === null) {
83
                    $message = $prefix . ' not exists. Uploading new...';
84
                    $page    = new DocsPage(['identify' => $file->getId()]);
85
                }
86
87
                $this->comment($message);
88
                $this->updatePage($repo, $page, $file);
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param Docs              $repo
95
     * @param DocsPage          $page
96
     * @param DocsFileInterface $file
97
     */
98
    private function updatePage(Docs $repo, DocsPage $page, DocsFileInterface $file)
99
    {
100
        $page->docs()->associate($repo);
101
102
        if ($page->category_id === null) {
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<App\Models\DocsPage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
            $page->category_id = 0;
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<App\Models\DocsPage>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
        }
105
106
        $page->hash = $file->getHash();
0 ignored issues
show
Documentation introduced by
The property hash does not exist on object<App\Models\DocsPage>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
        $page->title = $file->getTitle();
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Models\DocsPage>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
        $page->content_source = $file->getContent();
0 ignored issues
show
Documentation introduced by
The property content_source does not exist on object<App\Models\DocsPage>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
110
        $page->save();
111
    }
112
}
113