Completed
Pull Request — master (#128)
by Ali
01:44
created

StubFileManager::getRenderedStub()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope;
4
5
use InvalidArgumentException;
6
7
class StubFileManager
8
{
9
    const STUBS_FOLDER_NAME = 'Stubs';
10
11
    const STUBS_FILE_FORMAT = '.stub';
12
    
13
    /**
14
     * Render The Stub With Passed Paramt into typeHintClassPath
15
     * 
16
     * @param string $stubName
0 ignored issues
show
Bug introduced by
There is no parameter named $stubName. 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...
17
     * 
18
     * @param array $keysMap
19
     * 
20
     * @return string
21
     */
22
    public static function getRenderedStub($stubFileName, $keysMap = null)
23
    {
24
        $mapping = [];
25
26
        $stubContent = self::getStubContentByFileName($stubFileName);
27
28
        // When Stub type is Staitc Stub
29
        if (is_null($keysMap)) {
30
            return $stubContent;
31
        }
32
33
        foreach ($keysMap as $key => $map) {
34
35
            $normalizedKey = str_replace('$', '', $key);
36
37
            $searchKey = "{{" . '$' . $normalizedKey . "}}";
38
39
            $mapping[$searchKey] = $map;
40
        }
41
42
        return str_replace(array_keys($mapping), array_values($mapping), $stubContent);
43
    }
44
45
    /**
46
     * Get the Stubs Content When Stub File is Exists!
47
     * 
48
     * @param string $stubName
49
     * 
50
     * @throws InvalidArgumentException
51
     * 
52
     * @return string
53
     */
54
    private static function getStubContentByFileName($stubName)
55
    {
56
        $stubName = str_replace(self::STUBS_FILE_FORMAT, '', $stubName);
57
58
        $stubFilePath = __DIR__ . DIRECTORY_SEPARATOR . self::STUBS_FOLDER_NAME . DIRECTORY_SEPARATOR . $stubName . self::STUBS_FILE_FORMAT;
59
60
        if (!(file_exists($stubFilePath) && is_readable($stubFilePath))) {
61
            throw new InvalidArgumentException("$stubName is not found in stubs folder");
62
        }
63
64
        return file_get_contents($stubFilePath);
65
    }
66
}
67