Skip to content

Commit ca5ada6

Browse files
committed
Improve quallity
1 parent f313f5d commit ca5ada6

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

Container.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
namespace MaplePHP\Container;
66

7+
use Closure;
78
use MaplePHP\Container\Interfaces\ContainerInterface;
89
use MaplePHP\Container\Interfaces\FactoryInterface;
910
use MaplePHP\DTO\Format\Arr;
10-
use MaplePHP\Container\Reflection;
11+
//use MaplePHP\Container\Reflection;
1112
use MaplePHP\Container\Exceptions\NotFoundException;
1213
use MaplePHP\Container\Exceptions\ContainerException;
14+
use ReflectionException;
1315

1416
class Container implements ContainerInterface, FactoryInterface
1517
{
16-
private $services = array();
17-
private $args;
18-
//private $overwrite;
19-
private $getter = array();
20-
18+
private array $services = [];
19+
private array $args = [];
20+
private array $getter = [];
2121

22+
/**
23+
* @throws ReflectionException
24+
*/
2225
public function __call($method, $args)
2326
{
2427
return $this->get($method, $args);
@@ -32,14 +35,14 @@ public function __call($method, $args)
3235
* TestClasses\Test::class."::__construct",
3336
* TestClasses\Test::class."::getStaticMethod",
3437
* @param array|null $args Pass argumnets to constructor staticMethod if you choose.
35-
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
38+
* @param bool $overwrite Will throw exception if already been defined if not arg is set to TRUE.
3639
*/
3740
public function set(string $identifier, $value, ?array $args = null, bool $overwrite = false): ContainerInterface
3841
{
3942
if (!$overwrite && $this->has($identifier)) {
4043
$type = ($this->isFactory($identifier)) ? "factory" : "container";
41-
throw new ContainerException("The {$type} ({$identifier}) has already been defined. If you want to overwrite " .
42-
"the {$type} then set overwrite argument to true.", 1);
44+
throw new ContainerException("The $type ($identifier) has already been defined. If you want to overwrite " .
45+
"the $type then set overwrite argument to true.", 1);
4346
}
4447

4548
if (isset($this->getter[$identifier])) {
@@ -54,18 +57,18 @@ public function set(string $identifier, $value, ?array $args = null, bool $overw
5457
* Same as @set, BUT will only accept a factory
5558
* @param string $identifier Uniq identifier
5659
* @param callable $factory
57-
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
60+
* @param bool $overwrite Will throw exception if already been defined if not arg is set to TRUE.
5861
* @return self
5962
*/
6063
public function factory(string $identifier, callable $factory, bool $overwrite = false): self
6164
{
6265
if (!$overwrite && $this->has($identifier)) {
6366
if (!$this->isFactory($identifier)) {
64-
throw new ContainerException("({$identifier}) Has already been defined, but has been defined as a " .
67+
throw new ContainerException("($identifier) Has already been defined, but has been defined as a " .
6568
"container and not factory. If you want to overwrite the container as factory then set " .
6669
"overwrite argument to true.", 1);
6770
} else {
68-
throw new ContainerException("The factory ({$identifier}) has already been defined. If you want to " .
71+
throw new ContainerException("The factory ($identifier) has already been defined. If you want to " .
6972
"overwrite the factory then set overwrite argument to true.", 1);
7073
}
7174
}
@@ -92,26 +95,27 @@ public function has(string $identifier): bool
9295
* @param string $identifier Uniq identifier
9396
* @return boolean
9497
*/
95-
public function isFactory(string $identifier)
98+
public function isFactory(string $identifier): bool
9699
{
97-
return ($this->getService($identifier) instanceof \Closure);
100+
return ($this->getService($identifier) instanceof Closure);
98101
}
99102

100103
/**
101104
* Check if is a container
102-
* @param string $identifier Uniq identifier
105+
* @param string $identifier Uniq identifier
103106
* @return boolean
104107
*/
105-
public function isContainer($identifier)
108+
public function isContainer(string $identifier): bool
106109
{
107110
return (!$this->isFactory($identifier));
108111
}
109112

110113
/**
111114
* Get a container or factory
112-
* @param string $identifier [description]
113-
* @param array $args Is possible to overwrite/add __construct or method argumnets
115+
* @param string $identifier [description]
116+
* @param array $args Is possible to overwrite/add __construct or method argumnets
114117
* @return mixed
118+
* @throws ReflectionException
115119
*/
116120
public function get(string $identifier, array $args = []): mixed
117121
{
@@ -136,22 +140,23 @@ public function get(string $identifier, array $args = []): mixed
136140
}
137141
return $this->getter[$identifier];
138142
} else {
139-
throw new NotFoundException("Trying to get a container ({$identifier}) that does not exists", 1);
143+
throw new NotFoundException("Trying to get a container ($identifier) that does not exists", 1);
140144
}
141145
}
142146

143147

144148
/**
145149
* Fetch is used to load multiple container and factories at once with the help of a wildcard search
150+
* @param string $identifier
151+
* @return array
152+
* @throws ReflectionException
146153
* @example @set("event.session", \name\space\session::class)
147154
* ->set("event.traverse", \name\space\traverse::class)
148155
* ->fetch("event.*");
149-
* @param string $identifier
150-
* @return mixed
151156
*/
152-
public function fetch(string $identifier)
157+
public function fetch(string $identifier): array
153158
{
154-
if (strpos($identifier, "*") !== false) {
159+
if (str_contains($identifier, "*")) {
155160
$arr = Arr::value($this->services)->wildcardSearch($identifier)->get();
156161
if (count($arr) > 0) {
157162
$new = array();

EventHandler.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace MaplePHP\Container;
66

7+
use Exception;
78
use MaplePHP\Container\Interfaces\EventInterface;
8-
use MaplePHP\Container\Reflection;
9-
use BadMethodCallException;
9+
use ReflectionException;
1010

1111
/**
12-
* Create an event handler that will listent to a certain method in class
12+
* Create an event handler that will listen to a certain method in class.
13+
* Note: This will change to a valid PSR-14 event dispatcher library in the future.
1314
*/
1415
class EventHandler
1516
{
@@ -21,7 +22,9 @@ class EventHandler
2122
/**
2223
* Add a class handler that you want to listen to
2324
* @param object|string $handler
25+
* @param string|array|null $method
2426
* @return void
27+
* @throws ReflectionException
2528
*/
2629
public function addHandler(object|string $handler, string|array $method = null): void
2730
{
@@ -39,9 +42,11 @@ public function addHandler(object|string $handler, string|array $method = null):
3942

4043
/**
4144
* Attach an event to a method that exist in handler
42-
* @param string $method
43-
* @param callable $event
45+
* @param callable|object|string $event
46+
* @param string|null $bind
4447
* @return void
48+
* @throws ReflectionException
49+
* @throws Exception
4550
*/
4651
public function addEvent(callable|object|string $event, ?string $bind = null): void
4752
{
@@ -53,7 +58,7 @@ public function addEvent(callable|object|string $event, ?string $bind = null): v
5358
}
5459

5560
if(is_object($event) && !($event instanceof EventInterface)) {
56-
throw new \Exception("Event object/class needs to be instance of \"EventInterface\"!", 1);
61+
throw new Exception("Event object/class needs to be instance of \"EventInterface\"!", 1);
5762
}
5863
}
5964

@@ -74,12 +79,12 @@ public function stopPropagation(bool $stopPropagate): void
7479
$this->stopPropagate = $stopPropagate;
7580
}
7681

77-
7882
/**
7983
* Release the listener
80-
* @param string $method
81-
* @param array $args
84+
* @param string $method
85+
* @param array $args
8286
* @return mixed
87+
* @throws Exception
8388
*/
8489
public function __call(string $method, array $args): mixed
8590
{
@@ -108,10 +113,7 @@ public function __call(string $method, array $args): mixed
108113
*/
109114
final protected function triggerEvents(): void
110115
{
111-
if (is_null($this->event)) {
112-
throw new \Exception("Event has not been initiated", 1);
113-
}
114-
foreach ($this->event as $key => $event) {
116+
if (is_array($this->event)) foreach ($this->event as $key => $event) {
115117
if (is_array($event)) {
116118
$select = key($event);
117119
$this->getEvent($event[$select]);

0 commit comments

Comments
 (0)