Passed
Pull Request — master (#49)
by Chubarov
02:56
created

ConversationCreator::filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Helpers\Str;
8
use League\Flysystem\MountManager;
9
10
class ConversationCreator
11
{
12
    private $filesystem;
13
14 2
    public function __construct(MountManager $manager)
15
    {
16 2
        $this->filesystem = $manager->getFilesystem('local');
17 2
    }
18
19
    /**
20
     * Create new intent.
21
     *
22
     * @param string $directory
23
     * @param string $namespace
24
     * @param string $name
25
     *
26
     * @throws \League\Flysystem\FileNotFoundException
27
     * @throws \League\Flysystem\FileExistsException
28
     */
29 1
    public function createIntent(string $directory, string $namespace, string $name): void
30
    {
31 1
        $contents = $this->filesystem->read('vendor/fondbot/framework/resources/stubs/Intent.stub');
32
33 1
        $className = $this->className($name, 'Intent');
34
35
        // Replace stub placeholders
36 1
        $this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace, 'Intents'));
0 ignored issues
show
Security Bug introduced by
It seems like $contents defined by $this->filesystem->read(...ces/stubs/Intent.stub') on line 31 can also be of type false; however, FondBot\Conversation\Con...r::replacePlaceholder() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
37 1
        $this->replacePlaceholder($contents, 'className', $className);
38 1
        $this->replacePlaceholder($contents, 'name', $this->formatName($name));
39
40 1
        $path = $directory.'/Intents/'.$this->filename($className);
41
42 1
        $this->filesystem->write($path, $contents);
43 1
    }
44
45
    /**
46
     * Create new interaction.
47
     *
48
     * @param string $directory
49
     * @param string $namespace
50
     * @param string $name
51
     *
52
     * @throws \League\Flysystem\FileNotFoundException
53
     * @throws \League\Flysystem\FileExistsException
54
     */
55 1
    public function createInteraction(string $directory, string $namespace, string $name): void
56
    {
57 1
        $contents = $this->filesystem->read('vendor/fondbot/framework/resources/stubs/Interaction.stub');
58
59 1
        $className = $this->className($name, 'Interaction');
60
61
        // Replace stub placeholders
62 1
        $this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace, 'Interactions'));
0 ignored issues
show
Security Bug introduced by
It seems like $contents defined by $this->filesystem->read(...tubs/Interaction.stub') on line 57 can also be of type false; however, FondBot\Conversation\Con...r::replacePlaceholder() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
63 1
        $this->replacePlaceholder($contents, 'className', $className);
64
65 1
        $path = $directory.'/Interactions/'.$this->filename($className);
66
67 1
        $this->filesystem->write($path, $contents);
68 1
    }
69
70
    /**
71
     * Replace placeholder.
72
     *
73
     * @param string $input
74
     * @param string $key
75
     * @param string $value
76
     */
77 2
    private function replacePlaceholder(string &$input, string $key, string $value): void
78
    {
79 2
        $key = mb_strtoupper($key);
80 2
        $input = str_replace('___'.$key.'___', $value, $input);
81 2
    }
82
83
    /**
84
     * Get filename.
85
     *
86
     * @param string $name
87
     *
88
     * @return string
89
     */
90 2
    private function filename(string $name): string
91
    {
92 2
        return $name.'.php';
93
    }
94
95
    /**
96
     * Get formatted name.
97
     *
98
     * @param string $name
99
     *
100
     * @return string
101
     */
102 1
    private function formatName(string $name): string
103
    {
104 1
        return mb_strtolower(trim($name));
105
    }
106
107
    /**
108
     * Get formatted namespace.
109
     *
110
     * @param string      $value
111
     *
112
     * @param string|null $postfix
113
     *
114
     * @return string
115
     */
116 2
    private function namespace(string $value, string $postfix = null): string
117
    {
118 2
        if (Str::endsWith($value, '\\')) {
119 2
            $value = mb_substr($value, 0, -1);
120
        }
121
122 2
        if ($postfix !== null) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $postfix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
123 2
            $value .= '\\'.$postfix;
124
        }
125
126 2
        return $value;
127
    }
128
129
    /**
130
     * Get name of class.
131
     *
132
     * @param string $name
133
     * @param string $postfix
134
     *
135
     * @return string
136
     */
137 2
    private function className(string $name, string $postfix): string
138
    {
139 2
        $name = trim($name);
140 2
        if (!Str::endsWith($name, $postfix)) {
141 2
            $name .= $postfix;
142
        }
143
144 2
        return $name;
145
    }
146
}
147