Skip to content

Commit 4ef33d1

Browse files
committed
chore: remove reflection from class name detection
1 parent a69fa21 commit 4ef33d1

8 files changed

Lines changed: 61 additions & 48 deletions

app/Audit/AuditLogOtlpStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private function resolveAuditableEntity($subject)
8888
{
8989

9090
// any collection → log the owner
91-
if ($subject instanceof \Doctrine\ORM\PersistentCollection) {
91+
if ($subject instanceof PersistentCollection) {
9292
return $subject->getOwner();
9393
}
9494

app/Audit/AuditLoggerFactory.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Illuminate\Support\Facades\Log;
1919
use ReflectionClass;
20+
use Throwable;
2021

2122
/**
2223
* Class ChildEntityFormatterFactory
@@ -28,14 +29,10 @@ class AuditLoggerFactory
2829
public static function build($entity): ?ILogger
2930
{
3031
try {
31-
$class_name = (new ReflectionClass($entity))->getShortName();
32-
$class_name = "App\\Audit\\Loggers\\{$class_name}AuditLogger";
33-
if (class_exists($class_name)) {
34-
return new $class_name();
35-
}
36-
Log::warning(sprintf("AuditLoggerFactory::build %s not found", $class_name));
37-
return null;
38-
} catch (\ReflectionException $e) {
32+
$short = class_basename(is_string($entity) ? $entity : get_class($entity));
33+
$class_name = "App\\Audit\\Loggers\\{$short}AuditLogger";
34+
return class_exists($class_name) ? new $class_name() : null;
35+
} catch (Throwable $e) {
3936
Log::error($e);
4037
return null;
4138
}

app/Audit/ConcreteFormatters/ChildEntityFormatters/ChildEntityFormatterFactory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
**/
1717

1818
use Illuminate\Support\Facades\Log;
19+
use Throwable;
1920

2021
/**
2122
* Class ChildEntityFormatterFactory
2223
* @package App\Audit\ConcreteFormatters\ChildEntityFormatter
2324
*/
24-
class ChildEntityFormatterFactory {
25+
class ChildEntityFormatterFactory
26+
{
2527

26-
public static function build(object|string $entity): ?IChildEntityAuditLogFormatter {
28+
public static function build(object|string $entity): ?IChildEntityAuditLogFormatter
29+
{
2730
try {
28-
$short = is_string($entity)
29-
? substr(ltrim($entity, '\\'), strrpos(ltrim($entity, '\\'), '\\') + 1)
30-
: (new \ReflectionClass($entity))->getShortName();
31-
Log::debug("ChildEntityFormatterFactory::build short {$short}");
31+
$short = class_basename(is_string($entity) ? $entity : get_class($entity));
3232
$class = "App\\Audit\\ConcreteFormatters\\ChildEntityFormatters\\{$short}AuditLogFormatter";
3333
return class_exists($class) ? new $class() : null;
3434

35-
} catch (\Throwable $e) {
35+
} catch (Throwable $e) {
3636
Log::error($e);
3737
return null;
3838
}
3939
}
40-
}
40+
}

app/Audit/ConcreteFormatters/EntityCollectionUpdateAuditLogFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatt
4040
/**
4141
* @inheritDoc
4242
*/
43-
public function format($subject, $change_set): ?string {
43+
public function format($subject, array $change_set): ?string
44+
{
4445
try {
4546
if ($this->child_entity_formatter != null) {
4647
$changes = [];
@@ -66,5 +67,4 @@ public function format($subject, $change_set): ?string {
6667
return null;
6768
}
6869
}
69-
}
70-
70+
}

app/Audit/ConcreteFormatters/EntityCreationAuditLogFormatter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
*/
2626
class EntityCreationAuditLogFormatter extends AbstractAuditLogFormatter
2727
{
28-
protected function getCreationIgnoredEntities(): array {
28+
protected function getCreationIgnoredEntities(): array
29+
{
2930
return [
3031
'PresentationAction',
3132
'PresentationExtraQuestionAnswer'
@@ -35,10 +36,12 @@ protected function getCreationIgnoredEntities(): array {
3536
/**
3637
* @inheritDoc
3738
*/
38-
public function format($subject, $change_set): ?string {
39-
$class_name = (new ReflectionClass($subject))->getShortName();
39+
public function format($subject, $change_set): ?string
40+
{
41+
$class_name = class_basename(is_string($subject) ? $subject : get_class($subject));
4042
$ignored_entities = $this->getCreationIgnoredEntities();
41-
if (in_array($class_name, $ignored_entities)) return null;
43+
if (in_array($class_name, $ignored_entities))
44+
return null;
4245
return "{$class_name} created";
4346
}
44-
}
47+
}

app/Audit/ConcreteFormatters/EntityDeletionAuditLogFormatter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatt
3636
$this->child_entity_formatter = $child_entity_formatter;
3737
}
3838

39-
protected function getCreationIgnoredEntities(): array {
39+
protected function getCreationIgnoredEntities(): array
40+
{
4041
return [
4142
'PresentationAction',
4243
'PresentationExtraQuestionAnswer'
@@ -46,10 +47,12 @@ protected function getCreationIgnoredEntities(): array {
4647
/**
4748
* @inheritDoc
4849
*/
49-
public function format($subject, $change_set): ?string {
50-
$class_name = (new ReflectionClass($subject))->getShortName();
50+
public function format($subject, $change_set): ?string
51+
{
52+
$class_name = class_basename(is_string($subject) ? $subject : get_class($subject));
5153
$ignored_entities = $this->getCreationIgnoredEntities();
52-
if (in_array($class_name, $ignored_entities)) return null;
54+
if (in_array($class_name, $ignored_entities))
55+
return null;
5356

5457
if ($this->child_entity_formatter != null) {
5558
return $this->child_entity_formatter
@@ -58,4 +61,4 @@ public function format($subject, $change_set): ?string {
5861

5962
return "{$class_name} deleted";
6063
}
61-
}
64+
}

app/Audit/ConcreteFormatters/EntityUpdateAuditLogFormatter.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace App\Audit\ConcreteFormatters;
1+
<?php
2+
namespace App\Audit\ConcreteFormatters;
23
/**
34
* Copyright 2022 OpenStack Foundation
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -86,11 +87,12 @@ private static function formatEntity(string $parent_class, string $prop_name, ?I
8687
public function format($subject, $change_set): ?string
8788
{
8889
$res = [];
89-
$class_name = (new ReflectionClass($subject))->getShortName();
90+
$class_name = class_basename(is_string($subject) ? $subject : get_class($subject));
9091
$ignored_fields = $this->getIgnoredFields();
9192

9293
foreach (array_keys($change_set) as $prop_name) {
93-
if (in_array($prop_name, $ignored_fields)) continue;
94+
if (in_array($prop_name, $ignored_fields))
95+
continue;
9496

9597
$change_values = $change_set[$prop_name];
9698

@@ -99,9 +101,11 @@ public function format($subject, $change_set): ?string
99101

100102
if ($this->child_entity_formatter != null) {
101103
$res[] = $this->child_entity_formatter
102-
->format($subject,
104+
->format(
105+
$subject,
103106
IChildEntityAuditLogFormatter::CHILD_ENTITY_UPDATE,
104-
"Property \"{$prop_name}\" has changed from \"{$old_value}\" to \"{$new_value}\"");
107+
"Property \"{$prop_name}\" has changed from \"{$old_value}\" to \"{$new_value}\""
108+
);
105109
continue;
106110
}
107111

@@ -155,8 +159,9 @@ public function format($subject, $change_set): ?string
155159
}
156160
}
157161

158-
if (count($res) == 0) return null;
162+
if (count($res) == 0)
163+
return null;
159164

160165
return join("|", $res);
161166
}
162-
}
167+
}

app/ModelSerializers/SerializerRegistry.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace App\ModelSerializers;
1+
<?php
2+
namespace App\ModelSerializers;
23
/**
34
* Copyright 2019 OpenStack Foundation
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,10 +42,12 @@ final class SerializerRegistry
4142
*/
4243
private static $instance;
4344

44-
const SerializerType_Public = 'PUBLIC';
45+
const SerializerType_Public = 'PUBLIC';
4546
const SerializerType_Private = 'PRIVATE';
4647

47-
private function __clone(){}
48+
private function __clone()
49+
{
50+
}
4851

4952
/**
5053
* @var IResourceServerContext
@@ -112,17 +115,19 @@ private function __construct()
112115
* @param string $type
113116
* @return IModelSerializer
114117
*/
115-
public function getSerializer($object, $type = self::SerializerType_Public){
116-
if(is_null($object)) return null;
117-
$reflect = new ReflectionClass($object);
118-
$class = $reflect->getShortName();
119-
if(!isset($this->registry[$class]))
120-
throw new \InvalidArgumentException('Serializer not found for '.$class);
118+
public function getSerializer($object, $type = self::SerializerType_Public)
119+
{
120+
if (is_null($object))
121+
return null;
122+
123+
$class = class_basename(is_string($object) ? $object : get_class($object));
124+
if (!isset($this->registry[$class]))
125+
throw new \InvalidArgumentException('Serializer not found for ' . $class);
121126

122127
$serializer_class = $this->registry[$class];
123128

124-
if(is_array($serializer_class)){
125-
if(!isset($serializer_class[$type]))
129+
if (is_array($serializer_class)) {
130+
if (!isset($serializer_class[$type]))
126131
throw new \InvalidArgumentException(sprintf('Serializer not found for %s , type %s', $class, $type));
127132
$serializer_class = $serializer_class[$type];
128133
}

0 commit comments

Comments
 (0)