• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / orm / 23255296146

18 Mar 2026 04:24PM UTC coverage: 47.104%. Remained the same
23255296146

push

github

BenMorel
Avoid \Exception that somehow confuses ECS

1 of 5 new or added lines in 1 file covered. (20.0%)

402 existing lines in 24 files now uncovered.

553 of 1174 relevant lines covered (47.1%)

10.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ProxyBuilder.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\ORM;
6

7
use ReflectionClass;
8
use ReflectionException;
9
use RuntimeException;
10

11
use function array_map;
12
use function array_unique;
13
use function array_values;
14
use function count;
15
use function file_get_contents;
16
use function implode;
17
use function str_repeat;
18
use function str_replace;
19
use function var_export;
20

21
class ProxyBuilder
22
{
23
    private ?string $proxyNamespace = null;
24

25
    /**
26
     * @var class-string|null
27
     */
28
    private ?string $entityClassName = null;
29

30
    /**
31
     * @var list<string>|null
32
     */
33
    private ?array $nonIdProps = null;
34

35
    /**
36
     * @param string $namespace The namespace of the proxy class.
37
     */
38
    public function setProxyNamespace(string $namespace): void
39
    {
UNCOV
40
        $this->proxyNamespace = $namespace;
×
41
    }
42

43
    /**
44
     * @param class-string $className The FQCN of the entity.
45
     */
46
    public function setEntityClassName(string $className): void
47
    {
48
        $this->entityClassName = $className;
×
49
    }
50

51
    /**
52
     * @param list<string> $props The list of non-identity properties.
53
     */
54
    public function setNonIdProps(array $props): void
55
    {
UNCOV
56
        $this->nonIdProps = $props;
×
57
    }
58

59
    /**
60
     * Builds and returns the proxy source code.
61
     *
62
     * @throws RuntimeException    If data are missing.
63
     * @throws ReflectionException If a class does not exist.
64
     */
65
    public function build(): string
66
    {
67
        if ($this->proxyNamespace === null) {
×
68
            throw new RuntimeException('Missing proxy namespace.');
×
69
        }
70

71
        if ($this->entityClassName === null) {
×
72
            throw new RuntimeException('Missing entity class name.');
×
73
        }
74

75
        if ($this->nonIdProps === null) {
×
UNCOV
76
            throw new RuntimeException('Missing non-id props.');
×
77
        }
78

79
        $imports = [
×
80
            $this->entityClassName,
×
UNCOV
81
        ];
×
82

83
        $entityClassShortName = (new ReflectionClass($this->entityClassName))->getShortName();
×
84

85
        $code = file_get_contents(__DIR__ . '/ProxyTemplate.php');
×
86

87
        $code = str_replace('PROXY_NAMESPACE', $this->proxyNamespace, $code);
×
UNCOV
88
        $code = str_replace('CLASS_NAME', $entityClassShortName, $code);
×
89

UNCOV
90
        if ($this->nonIdProps) {
×
91
            $unsets = "\n";
×
92

93
            $unsets .= implode(",\n", array_map(static function (string $prop): string {
×
UNCOV
94
                return str_repeat(' ', 12) . '$this->' . $prop;
×
UNCOV
95
            }, $this->nonIdProps));
×
96

97
            $unsets .= "\n" . str_repeat(' ', 8);
×
98

UNCOV
99
            $code = str_replace('$UNSET_NON_ID_PROPS', $unsets, $code);
×
100
        } else {
UNCOV
101
            $code = str_replace('unset($UNSET_NON_ID_PROPS);', '', $code);
×
102
        }
103

104
        $nonIdProps = array_map(static function (string $prop): string {
×
UNCOV
105
            return var_export($prop, true);
×
106
        }, $this->nonIdProps);
×
107

108
        $code = str_replace('NON_ID_PROPS', implode(', ', $nonIdProps), $code);
×
109

110
        // Imports
111

UNCOV
112
        $importString = '';
×
113

UNCOV
114
        $imports = array_values(array_unique($imports));
×
115

116
        foreach ($imports as $key => $import) {
×
UNCOV
117
            if ($key !== 0) {
×
UNCOV
118
                $importString .= '    ';
×
119
            }
120

UNCOV
121
            $importString .= $import;
×
122

UNCOV
123
            if ($key !== count($imports) - 1) {
×
UNCOV
124
                $importString .= ",\n";
×
125
            }
126
        }
127

UNCOV
128
        $code = str_replace('IMPORTS', $importString, $code);
×
129

UNCOV
130
        return $code;
×
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc