GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 77b895...33e092 )
by Freek
11:18
created

MakeAggregateRootCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\LaravelEventSauce\Commands;
4
5
use Illuminate\Console\Command;
6
use EventSauce\EventSourcing\CodeGeneration\CodeDumper;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Str;
9
use ReflectionClass;
10
use Spatie\LaravelEventSauce\Exceptions\CouldNotMakeAggregateRoot;
11
use Spatie\LaravelEventSauce\Exceptions\InvalidConfiguration;
12
use EventSauce\EventSourcing\CodeGeneration\YamlDefinitionLoader;
13
14
class MakeAggregateRootCommand extends Command
15
{
16
    protected $signature = 'make:aggregate-root {class}';
17
18
    protected $description = 'Create a new aggregate root class';
19
20
    /** @var \Illuminate\Filesystem\Filesystem*/
21
    protected $files;
22
23
    public function __construct(Filesystem $files)
24
    {
25
        parent::__construct();
26
27
        $this->files = $files;
0 ignored issues
show
Documentation Bug introduced by
It seems like $files of type object<Illuminate\Filesystem\Filesystem> is incompatible with the declared type object<Illuminate\Filesystem\Filesystem*/> of property $files.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    public function handle()
31
    {
32
        $aggregateRootFqcn = $this->qualifyClass($this->argument('class'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('class') targeting Illuminate\Console\Command::argument() can also be of type array or null; however, Spatie\LaravelEventSauce...Command::qualifyClass() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
33
        $aggregateRootPath = $this->getPath($aggregateRootFqcn);
34
35
        $aggregateRootRepositoryFqcn = $this->qualifyClass($this->argument('class') . 'Repository');
36
        $aggregateRootRepositoryPath = $this->getPath($aggregateRootRepositoryFqcn);
37
38
        $this->ensureValidPaths([$aggregateRootPath, $aggregateRootRepositoryPath]);
39
40
        $this->makeDirectory($aggregateRootPath);
41
42
        $replacements = [
43
            'aggregateRootClass' => class_basename($aggregateRootFqcn),
44
            'namespace' => substr($aggregateRootFqcn, 0, strrpos( $aggregateRootFqcn, '\\')),
45
        ];
46
47
        $this->files->put($aggregateRootPath, $this->getClassContent('AggregateRoot', $replacements));
48
        $this->files->put($aggregateRootRepositoryPath, $this->getClassContent('AggregateRootRepository', $replacements));
49
50
        $this->info('Aggregate root created successfully!');
51
    }
52
53
    protected function getPath($name)
54
    {
55
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);
0 ignored issues
show
Documentation Bug introduced by
The method rootNamespace does not exist on object<Spatie\LaravelEve...keAggregateRootCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
57
        return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php';
58
    }
59
60
    /**
61
     * Parse the class name and format according to the root namespace.
62
     *
63
     * @param  string $name
64
     *
65
     * @return string
66
     */
67
    protected function qualifyClass($name)
68
    {
69
        $name = ltrim($name, '\\/');
70
71
        $rootNamespace = $this->laravel->getNamespace();
72
73
        if (Str::startsWith($name, $rootNamespace)) {
74
            return $name;
75
        }
76
77
        $name = str_replace('/', '\\', $name);
78
79
        return $this->qualifyClass(trim($rootNamespace, '\\') . '\\' . $name);
80
    }
81
82
    protected function ensureValidPaths(array $paths)
83
    {
84
        foreach ($paths as $path) {
85
            if (file_exists($path)) {
86
                throw CouldNotMakeAggregateRoot::fileAlreadyExists($path);
87
            };
88
        }
89
    }
90
91
    protected function makeDirectory($path)
92
    {
93
        if (! $this->files->isDirectory(dirname($path))) {
94
            $this->files->makeDirectory(dirname($path), 0777, true, true);
95
        }
96
97
        return $path;
98
    }
99
100
    protected function getClassContent(string $stubName, array $replacements)
101
    {
102
        $content = $this->files->get(__DIR__ . "/stubs/{$stubName}.php.stub");
103
104
        foreach($replacements as $search => $replace)
105
        {
106
            $content = str_replace("{{ {$search} }}", $replace, $content);
107
        }
108
109
        return $content;
110
    }
111
}
112