Completed
Push — master ( 8b7814...17b208 )
by Vitaly
02:28
created

ApplicationWriter::write()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 10

Duplication

Lines 27
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 27
loc 27
rs 8.439
cc 5
eloc 10
nc 8
nop 0
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 View Code Duplication
    public function write()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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
        foreach ($this->analyzers as $analyzerClass => $analyzer) {
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