Test Failed
Push — main ( e653bf...1d729b )
by Garbuz
03:20
created

FileGenerator::spacer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Builder;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
8
use Illuminate\Support\Facades\File;
9
use OpenApi\Annotations\Components;
0 ignored issues
show
Bug introduced by
The type OpenApi\Annotations\Components was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class FileGenerator
12
{
13
    /**
14
     * @var Configuration
15
     */
16
    private Configuration $config;
17
    /**
18
     * @var Package
19
     */
20
    private Package $package;
21
22 2
    public function __construct(Configuration $config)
23
    {
24 2
        $this->config = $config;
25 2
    }
26
27
    /**
28
     * @param Package $package
29
     * @return bool
30
     */
31 2
    public function make(Package $package): bool
32
    {
33 2
        $this->package = $package;
34 2
        $this->templates();
35 2
        return true;
36
    }
37
38
    /**
39
     * Copy all templates file
40
     *
41
     * @return bool
42
     */
43 2
    public function templates(): bool
44
    {
45 2
        $templatesDir = realpath(__DIR__ . '/../../templates');
46 2
        $files = File::allFiles($templatesDir, true);
47 2
        foreach ($files as $file) {
48 2
            $content = $this->replacement(file_get_contents($file->getRealPath()));
49 2
            $newPathFile = str_replace([$templatesDir, '.stub'], null, $file->getRealPath());
50 2
            $newPathFile = $this->package->getPath($newPathFile);
51 2
            file_put_contents($newPathFile, $content);
52
        }
53 2
        return true;
54
    }
55
56
    /**
57
     * @param string|null $content
58
     * @return string|null
59
     */
60 2
    public function replacement(?string $content = null): ?string
61
    {
62 2
        $content = str_replace([
63 2
            '%PACKAGE_GIT%',
64
            '%PACKAGE_PVENDOR%',
65
            '%PACKAGE_PNAME%',
66
            '%PACKAGE_NAMESPACE%',
67
            '%PACKAGE_NAMESPACE_SLASHES%',
68
            '%PACKAGE_CONFIG_NAME%',
69
            '%PACKAGE_NAME%',
70
            '%PACKAGE_DESCRIPTIONS%',
71
        ], [
72 2
            $this->package->getPackageVendor() . '/' . $this->package->getPackageName(),
73 2
            $this->package->getPackageVendor(),
74 2
            $this->package->getPackageName(),
75 2
            $this->spacer($this->package->getPackageVendor()) . '\\' . $this->spacer($this->package->getPackageName()),
0 ignored issues
show
Bug introduced by
It seems like $this->package->getPackageVendor() can also be of type null; however, parameter $name of GarbuzIvan\LaravelGenera...FileGenerator::spacer() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            $this->spacer(/** @scrutinizer ignore-type */ $this->package->getPackageVendor()) . '\\' . $this->spacer($this->package->getPackageName()),
Loading history...
76 2
            $this->spacer($this->package->getPackageVendor()) . '\\\\' . $this->spacer($this->package->getPackageName()),
77 2
            $this->package->getPackageVendor() . '-' . $this->package->getPackageName(),
78 2
            $this->package->getName(),
79 2
            $this->package->getDescripton(),
80
        ], $content);
81 2
        return $content;
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return string
87
     */
88 2
    public function spacer(string $name): string
89
    {
90 2
        $name = preg_replace('~[^a-z0-9]~isuU', ' ', $name);
91 2
        $name = ucwords($name);
92 2
        $name = str_replace(' ', null, $name);
93 2
        return $name;
94
    }
95
}
96