GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0b4676...ecfcbd )
by joseph
17s queued 14s
created

EntityFormatter   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 101
ccs 0
cts 72
cp 0
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A entityFileGenerator() 0 10 2
B getTraitType() 0 24 7
A __construct() 0 3 1
A run() 0 5 2
A setPathToProjectRoot() 0 5 1
A organiseTraits() 0 15 3
A getEntityBody() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File;
7
use Symfony\Component\Finder\Finder;
8
9
class EntityFormatter
10
{
11
    /**
12
     * @var FileFactory
13
     */
14
    private $fileFactory;
15
    /**
16
     * @var string
17
     */
18
    private $pathToProjectRoot;
19
20
    public function __construct(FileFactory $fileFactory)
21
    {
22
        $this->fileFactory = $fileFactory;
23
    }
24
25
    public function run(): void
26
    {
27
        foreach ($this->entityFileGenerator() as $entityFile) {
28
            $this->organiseTraits($entityFile);
29
            $entityFile->putContents();
30
        }
31
    }
32
33
    /**
34
     * @return \Generator|File[]
35
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
36
     */
37
    private function entityFileGenerator(): \Generator
38
    {
39
        $finder = new Finder();
40
        $finder->in($this->pathToProjectRoot . '/src/Entities');
41
        $finder->ignoreDotFiles(true);
42
        $finder->files();
43
        foreach ($finder as $file) {
44
            $entityFile = $this->fileFactory->createFromExistingPath($file->getPathname());
45
            $entityFile->loadContents();
46
            yield $entityFile;
47
        }
48
    }
49
50
    private function organiseTraits(File $entityFile): void
51
    {
52
        $body = $this->getEntityBody($entityFile);
53
        preg_match_all('%use [^;]+;%', $body, $traitMatches);
54
        $traits = [];
55
        foreach ($traitMatches[0] as $traitLine) {
56
            $traits[$this->getTraitType($traitLine)][] = $traitLine;
57
        }
58
        ksort($traits);
59
        $organisedBody = '';
60
        foreach ($traits as $title => $lines) {
61
            $organisedBody .= "\n" . substr($title, 2);
62
            $organisedBody .= "\n    " . implode("\n    ", $lines) . "\n";
63
        }
64
        $entityFile->setContents(str_replace($body, $organisedBody, $entityFile->getContents()));
65
    }
66
67
    private function getEntityBody(File $entityFile): string
68
    {
69
        preg_match('%class[^{].+?{(.+)}%s', $entityFile->getContents(), $matches);
70
71
        return $matches[1];
72
    }
73
74
    private function getTraitType(string $traitLine): string
75
    {
76
        $commentStart = "    /**\n     *";
77
        $commentEnd   = "\n     */";
78
        if (false !== \ts\stringContains($traitLine, 'Embeddable')) {
0 ignored issues
show
introduced by
The condition false !== stringContains...raitLine, 'Embeddable') is always true.
Loading history...
79
            return "5 $commentStart Embeddables $commentEnd";
80
        }
81
        if (false !== \ts\stringContains($traitLine, 'use HasRequired')) {
82
            return "1 $commentStart Required Relations $commentEnd";
83
        }
84
        if (false !== \ts\stringContains($traitLine, 'use Has')) {
85
            return "2 $commentStart Relations $commentEnd";
86
        }
87
        if (false !== \ts\stringContains($traitLine, 'use DSM\\Traits')) {
88
            return "0 $commentStart DSM Traits $commentEnd";
89
        }
90
        if (false !== \ts\stringContains($traitLine, 'use DSM\\Fields')) {
91
            return "3 $commentStart DSM Fields $commentEnd";
92
        }
93
        if (false !== \ts\stringContains($traitLine, 'FieldTrait')) {
94
            return "4 $commentStart Fields $commentEnd";
95
        }
96
97
        throw new \RuntimeException('Failed finding trait type for line ' . $traitLine);
98
    }
99
100
    /**
101
     * @param string $pathToProjectRoot
102
     *
103
     * @return EntityFormatter
104
     */
105
    public function setPathToProjectRoot(string $pathToProjectRoot): EntityFormatter
106
    {
107
        $this->pathToProjectRoot = $pathToProjectRoot;
108
109
        return $this;
110
    }
111
}
112