Skip to content

Commit e7684a1

Browse files
committed
refactor(Context): Returns null instead of throwing exception if the member is not found.
1 parent eeef3cc commit e7684a1

12 files changed

+27
-71
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 0.8.0
4+
5+
* refactor(`Context`): Returns null instead of throwing exception if the
6+
member is not found.
7+
38
## 0.7.3
49

510
* test: Add tests for read-only targets.

src/Context/Context.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,22 @@ public function without(object|string $value): self
7474
/**
7575
* @template T of object
7676
* @param class-string<T> $class
77-
* @return T
78-
* @throws ContextMemberNotFoundException
77+
* @return T|null
7978
*/
80-
public function get(string $class): object
79+
public function get(string $class): ?object
8180
{
8281
// @phpstan-ignore-next-line
83-
return $this->context[$class] ?? throw new ContextMemberNotFoundException();
82+
return $this->context[$class] ?? null;
8483
}
8584

8685
/**
8786
* @template T of object
8887
* @param class-string<T> $class
89-
* @return T
88+
* @return T|null
9089
*/
91-
public function __invoke(string $class): object
90+
public function __invoke(string $class): ?object
9291
{
9392
// @phpstan-ignore-next-line
94-
return $this->context[$class] ?? throw new ContextMemberNotFoundException();
93+
return $this->context[$class] ?? null;
9594
}
9695
}

src/Context/ContextMemberNotFoundException.php

-20
This file was deleted.

src/Debug/TraceableTransformer.php

+6-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Rekalogika\Mapper\Debug;
1515

1616
use Rekalogika\Mapper\Context\Context;
17-
use Rekalogika\Mapper\Context\ContextMemberNotFoundException;
1817
use Rekalogika\Mapper\MainTransformer\MainTransformerInterface;
1918
use Rekalogika\Mapper\MainTransformer\Model\DebugContext;
2019
use Rekalogika\Mapper\MainTransformer\Model\Path;
@@ -65,17 +64,12 @@ public function transform(
6564
?Type $targetType,
6665
Context $context
6766
): mixed {
68-
try {
69-
$path = $context(Path::class)->getLast();
70-
} catch (ContextMemberNotFoundException) {
71-
$path = null;
72-
}
67+
$path = $context(Path::class)?->getLast();
7368

74-
try {
75-
$debugContext = $context(DebugContext::class);
69+
if ($debugContext = $context(DebugContext::class)) {
7670
$possibleTargetTypes = $debugContext->getTargetTypes();
7771
$sourceTypeGuessed = $debugContext->isSourceTypeGuessed();
78-
} catch (ContextMemberNotFoundException) {
72+
} else {
7973
$possibleTargetTypes = null;
8074
$sourceTypeGuessed = false;
8175
}
@@ -90,12 +84,11 @@ public function transform(
9084
sourceTypeGuessed: $sourceTypeGuessed
9185
);
9286

93-
try {
94-
// add trace data to parent trace data
95-
$parentTraceData = $context(TraceData::class);
87+
// add trace data to parent trace data
88+
if ($parentTraceData = $context(TraceData::class)) {
9689
$parentTraceData->addNestedTraceData($traceData);
9790
$context = $context->with($traceData);
98-
} catch (ContextMemberNotFoundException) {
91+
} else {
9992
// @phpstan-ignore-next-line
10093
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
10194
$caller = $backtrace[2];

src/Exception/ContextAwareExceptionTrait.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Rekalogika\Mapper\Exception;
1515

1616
use Rekalogika\Mapper\Context\Context;
17-
use Rekalogika\Mapper\Context\ContextMemberNotFoundException;
1817
use Rekalogika\Mapper\MainTransformer\Model\Path;
1918

2019
trait ContextAwareExceptionTrait
@@ -25,11 +24,7 @@ public function __construct(
2524
?\Throwable $previous = null,
2625
protected ?Context $context = null,
2726
) {
28-
try {
29-
$path = $context?->get(Path::class);
30-
} catch (ContextMemberNotFoundException) {
31-
$path = null;
32-
}
27+
$path = $context?->get(Path::class);
3328

3429
$path = (string) $path;
3530
if ($path === '') {

src/MainTransformer/MainTransformer.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Rekalogika\Mapper\MainTransformer;
1515

1616
use Rekalogika\Mapper\Context\Context;
17-
use Rekalogika\Mapper\Context\ContextMemberNotFoundException;
1817
use Rekalogika\Mapper\MainTransformer\Exception\CannotFindTransformerException;
1918
use Rekalogika\Mapper\MainTransformer\Exception\CircularReferenceException;
2019
use Rekalogika\Mapper\MainTransformer\Exception\TransformerReturnsUnexpectedValueException;
@@ -96,31 +95,26 @@ public function transform(
9695

9796
// get or create object cache
9897

99-
try {
100-
$objectCache = $context(ObjectCache::class);
101-
} catch (ContextMemberNotFoundException) {
98+
if (!($objectCache = $context(ObjectCache::class))) {
10299
$objectCache = $this->objectCacheFactory->createObjectCache();
103100
$context = $context->with($objectCache);
104101
}
105102

106103
// initialize path it it doesn't exist
107104

108-
try {
109-
$pathContext = $context(Path::class);
110-
105+
if ($pathContext = $context(Path::class)) {
111106
// append path
112107

113108
if ($path === null) {
114109
$path = '(not specified)';
115110
}
116111

117112
$context = $context->with($pathContext->append($path));
118-
} catch (ContextMemberNotFoundException) {
113+
} else {
119114
$pathContext = Path::create();
120115
$context = $context->with($pathContext);
121116
}
122117

123-
124118
// determine the source type
125119

126120
if ($sourceType !== null) {

src/SubMapper/Implementation/SubMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function cache(mixed $target): void
131131
throw new CacheNotSupportedException($this->context);
132132
}
133133

134-
($this->context)(ObjectCache::class)->saveTarget(
134+
($this->context)(ObjectCache::class)?->saveTarget(
135135
source: $this->source,
136136
targetType: $this->targetType,
137137
target: $target

src/Transformer/ArrayToObjectTransformer.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Rekalogika\Mapper\Transformer;
1515

1616
use Rekalogika\Mapper\Context\Context;
17-
use Rekalogika\Mapper\Context\ContextMemberNotFoundException;
1817
use Rekalogika\Mapper\Exception\InvalidArgumentException;
1918
use Rekalogika\Mapper\Serializer\DenormalizerContext;
2019
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
@@ -53,11 +52,7 @@ public function transform(
5352
throw new InvalidTypeInArgumentException('Target type must be an object, "%s" given', $targetType, context: $context);
5453
}
5554

56-
try {
57-
$denormalizerContext = $context(DenormalizerContext::class)->toArray();
58-
} catch (ContextMemberNotFoundException) {
59-
$denormalizerContext = [];
60-
}
55+
$denormalizerContext = $context(DenormalizerContext::class)?->toArray() ?? [];
6156

6257
if ($target !== null) {
6358
if (!is_object($target)) {

src/Transformer/ObjectToArrayTransformer.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Rekalogika\Mapper\Transformer;
1515

1616
use Rekalogika\Mapper\Context\Context;
17-
use Rekalogika\Mapper\Context\ContextMemberNotFoundException;
1817
use Rekalogika\Mapper\Exception\InvalidArgumentException;
1918
use Rekalogika\Mapper\Serializer\NormalizerContext;
2019
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
@@ -46,11 +45,7 @@ public function transform(
4645
throw new InvalidArgumentException(sprintf('Source must be object, "%s" given', get_debug_type($source)), context: $context);
4746
}
4847

49-
try {
50-
$normalizerContext = $context(NormalizerContext::class)->toArray();
51-
} catch (ContextMemberNotFoundException) {
52-
$normalizerContext = [];
53-
}
48+
$normalizerContext = $context(NormalizerContext::class)?->toArray() ?? [];
5449

5550
return $this->normalizer->normalize(
5651
$source,

src/Transformer/ObjectToObjectTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function transform(
140140

141141
// save object to cache
142142

143-
$context(ObjectCache::class)->saveTarget(
143+
$context(ObjectCache::class)?->saveTarget(
144144
source: $source,
145145
targetType: $targetType,
146146
target: $target,

src/Transformer/TraversableToArrayAccessTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private function eagerTransform(
137137

138138
// Add the target to cache
139139

140-
$context(ObjectCache::class)->saveTarget(
140+
$context(ObjectCache::class)?->saveTarget(
141141
source: $source,
142142
targetType: $metadata->getTargetType(),
143143
target: $target,

src/Transformer/TraversableToTraversableTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function transform(
8989

9090
// Add to cache
9191

92-
$context(ObjectCache::class)->saveTarget(
92+
$context(ObjectCache::class)?->saveTarget(
9393
source: $source,
9494
targetType: $targetType,
9595
target: $target,

0 commit comments

Comments
 (0)