Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

LanguageFileService::buildFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Services;
4
5
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
6
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8
use Webfactor\Laravel\Generators\Traits\CanGenerateFile;
9
10
class LanguageFileService extends ServiceAbstract implements ServiceInterface
11
{
12
    use CanGenerateFile;
13
14
    protected $key = 'languageFile';
15
16
    /**
17
     * Build the language file.
18
     *
19
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
     *
21
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23
    private function buildFileContent()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
24
    {
25
        if ($this->filesystem->exists($this->naming->getFile())) {
26
            $translation = include $this->naming->getFile();
27
        }
28
29
        $translation = array_add($translation, $this->naming->getName(), [
0 ignored issues
show
Bug introduced by
The variable $translation does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
30
            'singular' => $this->naming->getSingular(),
31
            'plural'   => $this->naming->getPlural(),
32
        ]);
33
34
        $this->fileContent = $this->getTranslationFileContent($translation);
35
    }
36
37
    private function getTranslationFileContent(array $translation)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        $content = ShortSyntaxArray::parse($translation);
40
41
        return <<<FILE
42
<?php
43
        
44
return {$content};
45
FILE;
46
    }
47
}
48