ApplicationWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 57.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 19
loc 33
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B write() 19 27 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 25.03.16 at 12:04
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsonframework\orm\DatabaseInterface;
10
use samsonphp\generator\Generator;
11
12
/**
13
 * Generator classes file writer.
14
 *
15
 * @package samsoncms\api\generator
16
 */
17
class ApplicationWriter extends GenericWriter
18
{
19
    /**
20
     * Analyze, generate and write class files.
21
     */
22
    public function write()
23
    {
24
        // Create module cache folder if not exists
25
        if (!file_exists($this->path)) {
26
            @mkdir($path, 0777, true);
0 ignored issues
show
Bug introduced by
The variable $path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
27
        }
28
29 View Code Duplication
        foreach ($this->analyzers as $analyzerClass => $analyzer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            // Analyze database structure and get entities metadata
31
            foreach ($analyzer->analyze() as $metadata) {
32
                // Iterate all generators for analyzer
33
                foreach ($this->generators[$analyzerClass] as $generator) {
34
                    /** @var Generic $generator Create class generator */
35
                    $generator = new $generator($this->codeGenerator->defNamespace($this->namespace), $metadata);
0 ignored issues
show
Bug introduced by
The property namespace 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...
36
37
                    // Create entity generated class names
38
                    $file = $this->path . $generator->className . '.php';
39
40
                    // Create entity query class files
41
                    file_put_contents($file, '<?php' . $generator->generate());
42
43
                    // Require files
44
                    require($file);
45
                }
46
            }
47
        }
48
    }
49
}
50
//[PHPCOMPRESSOR(remove,end)]
51