Completed
Push — master ( f9fc64...b667fe )
by
unknown
28s queued 10s
created

getClassNamespace()   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 Kunstmaan\GeneratorBundle\Generator;
4
5
/**
6
 * @internal
7
 */
8
final class Symfony4EntityRepositoryGenerator
9
{
10
    protected static $_template =
11
        '<?php
12
13
namespace <namespace>;
14
15
use <entityClassName>;
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Symfony\Bridge\Doctrine\RegistryInterface;
18
19
class <repository> extends ServiceEntityRepository
20
{
21
    public function __construct(RegistryInterface $registry)
22
    {
23
        parent::__construct($registry, <entity>::class);
24
    }
25
}
26
';
27
28
    /**
29
     * @param string $entityClassName
0 ignored issues
show
Documentation introduced by
There is no parameter named $entityClassName. Did you maybe mean $entityClass?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
30
     * @param string $repositoryClassName
0 ignored issues
show
Documentation introduced by
There is no parameter named $repositoryClassName. Did you maybe mean $repositoryClass?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
31
     *
32
     * @return string
33
     */
34
    public function generateEntityRepositoryClass(string $entityClass, string $repositoryClass)
35
    {
36
        $variables = [
37
            '<namespace>' => $this->generateEntityRepositoryNamespace($repositoryClass),
38
            '<entityClassName>' => $entityClass,
39
            '<entity>' => $this->generateEntityName($entityClass),
40
            '<repository>' => $this->generateClassName($repositoryClass),
41
        ];
42
43
        return str_replace(array_keys($variables), array_values($variables), self::$_template);
44
    }
45
46
    /**
47
     * @param string $fullClassName
0 ignored issues
show
Bug introduced by
There is no parameter named $fullClassName. 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...
48
     * @param string $outputDirectory
49
     */
50
    public function writeEntityRepositoryClass($entityClass, $repositoryClass, $outputDirectory)
51
    {
52
        $classNameParts = explode('\\', $repositoryClass);
53
        $repositoryClassName = end($classNameParts);
54
        $this->repositoryName = $repositoryClassName;
0 ignored issues
show
Bug introduced by
The property repositoryName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        $code = $this->generateEntityRepositoryClass($entityClass, $repositoryClass);
56
57
        $path = $outputDirectory . DIRECTORY_SEPARATOR . 'Repository' . DIRECTORY_SEPARATOR . end($classNameParts) . '.php';
58
59
        $dir = dirname($path);
60
61
        if (!is_dir($dir)) {
62
            mkdir($dir, 0775, true);
63
        }
64
65
        if (!file_exists($path)) {
66
            file_put_contents($path, $code);
67
            chmod($path, 0664);
68
        }
69
    }
70
71
    /**
72
     * @param string $repositoryClassName
73
     *
74
     * @return bool|string
75
     */
76
    private function generateEntityRepositoryNamespace(string $repositoryClassName)
77
    {
78
        return substr($repositoryClassName, 0, strrpos($repositoryClassName, '\\'));
79
    }
80
81
    /**
82
     * @param string $entityClassName
83
     *
84
     * @return bool|string
85
     */
86
    private function generateEntityName(string $entityClassName)
87
    {
88
        return substr(strrchr($entityClassName, '\\'), 1);
89
    }
90
91
    /**
92
     * Generates the class name
93
     *
94
     * @param string $fullClassName
95
     *
96
     * @return string
97
     */
98
    private function generateClassName($fullClassName)
99
    {
100
        $namespace = $this->getClassNamespace($fullClassName);
101
102
        $className = $fullClassName;
103
104
        if ($namespace) {
105
            $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
106
        }
107
108
        return $className;
109
    }
110
111
    /**
112
     * Generates the namespace, if class do not have namespace, return empty string instead.
113
     *
114
     * @param string $fullClassName
115
     *
116
     * @return string $namespace
117
     */
118
    private function getClassNamespace($fullClassName)
119
    {
120
        $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
121
122
        return $namespace;
123
    }
124
}
125