Skip to content

Commit 6f33dda

Browse files
committed
Stop throwing exceptions.
The UnknownPropertyException being thrown whenever a non-existent property or method was requested caused a huge performance hit. Instead the library now returns null by default. This only affects method calls against the presenter which don't exist on the presenter or underlying model - null will be returned instead of throwing an exception. The only downside to this is a method you intended to define on a presenter but never did will not be brought to your attention with an exception when attempting to access the missing method.
1 parent 561a89a commit 6f33dda

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

src/Presenter/Exceptions/UnknownPropertyException.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Presenter/Presenter.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace Deefour\Presenter;
22

33
use BadMethodCallException;
4-
use Deefour\Presenter\Exceptions\UnknownPropertyException;
54
use Deefour\Presenter\Exceptions\NotDefinedException;
65
use Deefour\Presenter\Exceptions\NotPresentableException;
76
use Deefour\Presenter\Contracts\Presentable;
@@ -54,13 +53,7 @@ public function __get($property) {
5453
return $this->model; // don't decorate the model
5554
}
5655

57-
try {
58-
$value = $this->deriveReturnValue($property);
59-
} catch (UnknownPropertyException $e) {
60-
return null;
61-
}
62-
63-
return $this->wrapInPresenter($value);
56+
return $this->derive($property);
6457
}
6558

6659
/**
@@ -72,13 +65,7 @@ public function __get($property) {
7265
* @return mixed
7366
*/
7467
public function __call($method, array $args) {
75-
try {
76-
$value = $this->deriveReturnValue($method, $args);
77-
} catch (UnknownPropertyException $e) {
78-
throw new BadMethodCallException(sprintf('The `%s` method does not exist on the `%s` presenter or underlying `%s` model', $method, static::class, get_class($this->model)));
79-
}
80-
81-
return $this->wrapInPresenter($value);
68+
return $this->derive($method, $args);
8269
}
8370

8471
/**
@@ -99,7 +86,18 @@ public function __isset($property) {
9986
}
10087
}
10188

89+
/**
90+
* Derive the return value and wrap it in it's presenter if possible.
91+
*
92+
* @param string $property
93+
* @param array $args [optional]
94+
* @return mixed
95+
*/
96+
protected function derive($property, array $args = []) {
97+
$value = $this->deriveReturnValue($property, $args);
10298

99+
return $this->wrapInPresenter($value);
100+
}
103101

104102
/**
105103
* Snake-to-camel-case string conversion.
@@ -140,17 +138,16 @@ protected function deriveMethodName($property) {
140138
* @return mixed
141139
*/
142140
protected function deriveReturnValue($property, array $args = []) {
143-
$method = $this->deriveMethodName($property);
144-
145141
if (property_exists($this, $property)) {
146142
return $this->$property;
147143
}
148144

145+
$method = $this->deriveMethodName($property);
146+
149147
if (method_exists($this, $method)) {
150148
return call_user_func_array([ $this, $method ], $args);
151149
}
152150

153-
154151
if (isset($this->model->$property)) {
155152
return $this->model->$property;
156153
}
@@ -159,7 +156,7 @@ protected function deriveReturnValue($property, array $args = []) {
159156
return call_user_func_array([$this->model, $method], $args);
160157
}
161158

162-
throw new UnknownPropertyException(sprintf('No property or method could be derived from the passed `%s` attribute', $property));
159+
return null;
163160
}
164161

165162
/**

0 commit comments

Comments
 (0)