Completed
Push — master ( d75c1f...eed677 )
by Viacheslav
12s
created

PhpApp::addClass()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0322

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 14
nop 1
dl 0
loc 33
ccs 21
cts 23
cp 0.913
crap 7.0322
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder\App;
4
5
use Swaggest\CodeBuilder\App\App;
6
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder;
7
use Swaggest\PhpCodeBuilder\PhpClassTraitInterface;
8
use Swaggest\PhpCodeBuilder\PhpFile;
9
10
class PhpApp extends App
11
{
12
    /** @var PhpClassTraitInterface[] */
13
    private $classes = [];
0 ignored issues
show
introduced by
The private property $classes is not used, and could be removed.
Loading history...
14
    /** @var PhpFile[][] */
15
    private $phpFiles = [];
16
    /** @var PhpBuilder */
17
    private $builder;
18
19
    private $psr4Namespaces = [];
20
21 2
    public function setNamespaceRoot($namespace, $relativePath = './src/')
22
    {
23 2
        $relativePath = rtrim($relativePath, '/') . '/';
24
25 2
        $this->psr4Namespaces[$namespace] = $relativePath;
26 2
        return $this;
27
    }
28
29
    /**
30
     * @param PhpBuilder $builder
31
     * @return PhpApp
32
     */
33
    public function setBuilder($builder)
34
    {
35
        $this->builder = $builder;
36
        return $this;
37
    }
38
    
39 2
    public function addClass(PhpClassTraitInterface $class)
40
    {
41
        //$this->classes[] = $class;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
43 2
        if (!$class->getName()) {
44
            throw new \Exception('Can not store unnamed class');
45
        }
46 2
        $ns = $class->getNamespace();
47 2
        $filepath = $class->getName() . '.php';
48 2
        if ($ns) {
49 2
            $namespaceFound = false;
50 2
            foreach ($this->psr4Namespaces as $namespace => $relativePath) {
51 2
                $nns = substr($ns, 0, strlen($namespace));
52 2
                if ($namespace === $nns) {
53 2
                    $innerPath = str_replace('\\', '/', substr($ns, strlen($namespace)));
54 2
                    $innerPath = trim($innerPath, '/') . '/';
55 2
                    $filepath = $relativePath . $innerPath . $class->getName() . '.php';
56 2
                    $namespaceFound = true;
57 2
                    break;
58
                }
59
            }
60 2
            if (!$namespaceFound) {
61
                throw new \Exception('Namespace not found: ' . $ns);
62
            }
63
        }
64 2
        $file = new PhpFile();
65 2
        if ($ns) {
66 2
            $file->setNamespace($ns);
67
        }
68 2
        $file->getCode()->addSnippet($class);
69 2
        $this->files[$filepath] = $file;
70
71 2
        return $this;
72
    }
73
74
    public function addFile($contents, $relativePath)
75
    {
76
        $this->files[$relativePath] = $contents;
77
    }
78
79
    public function addPhpFile(PhpFile $file, $relativePath = './')
80
    {
81
        $this->phpFiles[$relativePath][] = $file;
82
        return $this;
83
    }
84
}