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
Pull Request — master (#154)
by joseph
33:02
created

EntityFormatter::organiseTraits()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 15
ccs 12
cts 12
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
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 2
    public function __construct(FileFactory $fileFactory)
21
    {
22 2
        $this->fileFactory = $fileFactory;
23 2
    }
24
25 2
    public function run(): void
26
    {
27 2
        foreach ($this->entityFileGenerator() as $entityFile) {
28 2
            $this->organiseTraits($entityFile);
29 2
            $entityFile->putContents();
30
        }
31 2
    }
32
33
    /**
34
     * @return \Generator|File[]
35
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
36
     */
37 2
    private function entityFileGenerator(): \Generator
38
    {
39 2
        $finder = new Finder();
40 2
        $finder->in($this->pathToProjectRoot . '/src/Entities');
41 2
        $finder->ignoreDotFiles(true);
42 2
        $finder->files();
43 2
        foreach ($finder as $file) {
44 2
            $entityFile = $this->fileFactory->createFromExistingPath($file->getPathname());
45 2
            $entityFile->loadContents();
46 2
            yield $entityFile;
47
        }
48 2
    }
49
50 2
    private function organiseTraits(File $entityFile): void
51
    {
52 2
        $body = $this->getEntityBody($entityFile);
53 2
        preg_match_all('%use [^;]+;%', $body, $traitMatches);
54 2
        $traits = [];
55 2
        foreach ($traitMatches[0] as $traitLine) {
56 2
            $traits[$this->getTraitType($traitLine)][] = $traitLine;
57
        }
58 2
        ksort($traits);
59 2
        $organisedBody = '';
60 2
        foreach ($traits as $title => $lines) {
61 2
            $organisedBody .= "\n" . substr($title, 2);
62 2
            $organisedBody .= "\n    " . implode("\n    ", $lines) . "\n";
63
        }
64 2
        $entityFile->setContents(str_replace($body, $organisedBody, $entityFile->getContents()));
65 2
    }
66
67 2
    private function getEntityBody(File $entityFile): string
68
    {
69 2
        preg_match('%class[^{].+?{(.+)}%s', $entityFile->getContents(), $matches);
70
71 2
        return $matches[1];
72
    }
73
74 2
    private function getTraitType(string $traitLine): string
75
    {
76 2
        $commentStart = "    /**\n     *";
77 2
        $commentEnd   = "\n     */";
78 2
        if (false !== \ts\stringContains($traitLine, 'Embeddable')) {
79 2
            return "5 $commentStart Embeddables $commentEnd";
80
        }
81 2
        if (false !== \ts\stringContains($traitLine, 'use HasRequired')) {
82 2
            return "1 $commentStart Required Relations $commentEnd";
83
        }
84 2
        if (false !== \ts\stringContains($traitLine, 'use Has')) {
85 2
            return "2 $commentStart Relations $commentEnd";
86
        }
87 2
        if (false !== \ts\stringContains($traitLine, 'use DSM\\Traits')) {
88 2
            return "0 $commentStart DSM Traits $commentEnd";
89
        }
90 2
        if (false !== \ts\stringContains($traitLine, 'use DSM\\Fields')) {
91 2
            return "3 $commentStart DSM Fields $commentEnd";
92
        }
93 2
        if (false !== \ts\stringContains($traitLine, 'FieldTrait')) {
94 2
            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 2
    public function setPathToProjectRoot(string $pathToProjectRoot): EntityFormatter
106
    {
107 2
        $this->pathToProjectRoot = $pathToProjectRoot;
108
109 2
        return $this;
110
    }
111
}
112