Passed
Pull Request — master (#20)
by Glynn
02:17
created
src/EventHandler.php 2 patches
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -8,96 +8,96 @@
 block discarded – undo
8 8
 
9 9
 class EventHandler
10 10
 {
11
-    /**
12
-     * @var array<string, array<string, Closure>>
13
-     */
14
-    protected $events = [];
11
+	/**
12
+	 * @var array<string, array<string, Closure>>
13
+	 */
14
+	protected $events = [];
15 15
 
16
-    /**
17
-     * @var string[]
18
-     */
19
-    protected $firedEvents = [];
16
+	/**
17
+	 * @var string[]
18
+	 */
19
+	protected $firedEvents = [];
20 20
 
21
-    /**
22
-     * @return array<string, array<string, Closure>>
23
-     */
24
-    public function getEvents()
25
-    {
26
-        return $this->events;
27
-    }
21
+	/**
22
+	 * @return array<string, array<string, Closure>>
23
+	 */
24
+	public function getEvents()
25
+	{
26
+		return $this->events;
27
+	}
28 28
 
29
-    /**
30
-     * @param string $event
31
-     * @param string|Raw $table
32
-     *
33
-     * @return Closure|null
34
-     */
35
-    public function getEvent(string $event, $table = ':any'): ?Closure
36
-    {
37
-        if ($table instanceof Raw) {
38
-            return null;
39
-        }
29
+	/**
30
+	 * @param string $event
31
+	 * @param string|Raw $table
32
+	 *
33
+	 * @return Closure|null
34
+	 */
35
+	public function getEvent(string $event, $table = ':any'): ?Closure
36
+	{
37
+		if ($table instanceof Raw) {
38
+			return null;
39
+		}
40 40
 
41
-        return $this->events[$table][$event] ?? null;
42
-    }
41
+		return $this->events[$table][$event] ?? null;
42
+	}
43 43
 
44
-    /**
45
-     * @param string $event
46
-     * @param string|null $table
47
-     * @param Closure $action
48
-     *
49
-     * @return void
50
-     */
51
-    public function registerEvent(string $event, ?string $table, Closure $action)
52
-    {
53
-        $table = $table ?? ':any';
44
+	/**
45
+	 * @param string $event
46
+	 * @param string|null $table
47
+	 * @param Closure $action
48
+	 *
49
+	 * @return void
50
+	 */
51
+	public function registerEvent(string $event, ?string $table, Closure $action)
52
+	{
53
+		$table = $table ?? ':any';
54 54
 
55
-        $this->events[$table][$event] = $action;
56
-    }
55
+		$this->events[$table][$event] = $action;
56
+	}
57 57
 
58
-    /**
59
-     * @param string $event
60
-     * @param string  $table
61
-     *
62
-     * @return void
63
-     */
64
-    public function removeEvent($event, $table = ':any')
65
-    {
66
-        unset($this->events[$table][$event]);
67
-    }
58
+	/**
59
+	 * @param string $event
60
+	 * @param string  $table
61
+	 *
62
+	 * @return void
63
+	 */
64
+	public function removeEvent($event, $table = ':any')
65
+	{
66
+		unset($this->events[$table][$event]);
67
+	}
68 68
 
69
-    /**
70
-     * @param QueryBuilderHandler $queryBuilder
71
-     * @param string $event
72
-     *
73
-     * @return mixed
74
-     */
75
-    public function fireEvents(QueryBuilderHandler $queryBuilder, string $event)
76
-    {
77
-        $statements = $queryBuilder->getStatements();
78
-        $tables     = $statements['tables'] ?? [];
69
+	/**
70
+	 * @param QueryBuilderHandler $queryBuilder
71
+	 * @param string $event
72
+	 *
73
+	 * @return mixed
74
+	 */
75
+	public function fireEvents(QueryBuilderHandler $queryBuilder, string $event)
76
+	{
77
+		$statements = $queryBuilder->getStatements();
78
+		$tables     = $statements['tables'] ?? [];
79 79
 
80
-        // Events added with :any will be fired in case of any table,
81
-        // we are adding :any as a fake table at the beginning.
82
-        array_unshift($tables, ':any');
80
+		// Events added with :any will be fired in case of any table,
81
+		// we are adding :any as a fake table at the beginning.
82
+		array_unshift($tables, ':any');
83 83
 
84
-        // Fire all events
85
-        foreach ($tables as $table) {
86
-            // Fire before events for :any table
87
-            if ($action = $this->getEvent($event, $table)) {
88
-                // Make an event id, with event type and table
89
-                $eventId = $event . $table;
84
+		// Fire all events
85
+		foreach ($tables as $table) {
86
+			// Fire before events for :any table
87
+			if ($action = $this->getEvent($event, $table)) {
88
+				// Make an event id, with event type and table
89
+				$eventId = $event . $table;
90 90
 
91
-                // Fire event
92
-                $handlerParams = func_get_args();
93
-                unset($handlerParams[1]); // we do not need $event
94
-                // Add to fired list
95
-                $this->firedEvents[] = $eventId;
96
-                $result              = call_user_func_array($action, $handlerParams);
97
-                if (!is_null($result)) {
98
-                    return $result;
99
-                }
100
-            }
101
-        }
102
-    }
91
+				// Fire event
92
+				$handlerParams = func_get_args();
93
+				unset($handlerParams[1]); // we do not need $event
94
+				// Add to fired list
95
+				$this->firedEvents[] = $eventId;
96
+				$result              = call_user_func_array($action, $handlerParams);
97
+				if (!is_null($result)) {
98
+					return $result;
99
+				}
100
+			}
101
+		}
102
+	}
103 103
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
             // Fire before events for :any table
87 87
             if ($action = $this->getEvent($event, $table)) {
88 88
                 // Make an event id, with event type and table
89
-                $eventId = $event . $table;
89
+                $eventId = $event.$table;
90 90
 
91 91
                 // Fire event
92 92
                 $handlerParams = func_get_args();
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
                 // Add to fired list
95 95
                 $this->firedEvents[] = $eventId;
96 96
                 $result              = call_user_func_array($action, $handlerParams);
97
-                if (!is_null($result)) {
97
+                if ( ! is_null($result)) {
98 98
                     return $result;
99 99
                 }
100 100
             }
Please login to merge, or discard this patch.
src/Hydration/Hydrator.php 2 patches
Indentation   +173 added lines, -176 removed lines patch added patch discarded remove patch
@@ -21,180 +21,177 @@
 block discarded – undo
21 21
  */
22 22
 class Hydrator
23 23
 {
24
-    /**
25
-     * The model to hydrate
26
-
27
-     *
28
-     * @var class-string<T>
29
-     */
30
-    protected $model;
31
-
32
-    /**
33
-     * The arguments used to create the new instance.
34
-     *
35
-     * @var array<string|int, mixed>
36
-     */
37
-    protected $constructorArgs;
38
-
39
-    /**
40
-
41
-     * @param class-string<T> $model
42
-     * @param array<string|int, mixed> $constructorArgs
43
-     */
44
-    public function __construct(string $model = stdClass::class, array $constructorArgs = [])
45
-    {
46
-        $this->model           = $model;
47
-        $this->constructorArgs = $constructorArgs;
48
-    }
49
-
50
-    /**
51
-     * Map many models
52
-     *
53
-     * @param array<int, object|mixed[]> $sources
54
-     *
55
-     * @return array<T>
56
-     */
57
-    public function fromMany(array $sources): array
58
-    {
59
-        return array_map([$this, 'from'], $sources);
60
-    }
61
-
62
-    /**
63
-     * Map a single model
64
-     *
65
-     * @param object|mixed[] $source
66
-     *
67
-     * @return T
68
-     */
69
-    public function from($source)
70
-    {
71
-        switch (true) {
72
-            case is_array($source):
73
-                return $this->fromArray($source);
74
-
75
-            case is_object($source):
76
-                return $this->fromObject($source);
77
-
78
-            default:
79
-                throw new Exception('Models can only be mapped from arrays or objects.', 1);
80
-        }
81
-    }
82
-
83
-    /**
84
-     * Maps the model from an array of data.
85
-     *
86
-     * @param array<string, mixed> $source
87
-     *
88
-     * @return T
89
-     */
90
-    protected function fromArray(array $source)
91
-    {
92
-        $model = $this->newInstance();
93
-        foreach ($source as $key => $value) {
94
-            $this->setProperty($model, $key, $value);
95
-        }
96
-
97
-        return $model;
98
-    }
99
-
100
-    /**
101
-     * Maps a model from an Object of data
102
-     *
103
-     * @param object $source
104
-     *
105
-     * @return T
106
-     */
107
-    protected function fromObject($source)
108
-    {
109
-        $vars = get_object_vars($source);
110
-
111
-        return $this->fromArray($vars);
112
-    }
113
-
114
-    /**
115
-     * Construct an instance of the model
116
-
117
-     *
118
-     * @return T
119
-     */
120
-    protected function newInstance()
121
-    {
122
-        $class = $this->model;
123
-        try {
124
-            /** @var T */
125
-            $instance = empty($this->constructorArgs)
126
-                ? new $class()
127
-                : new $class(...$this->constructorArgs);
128
-        } catch (Throwable $th) {
129
-            throw new Exception("Failed to construct model, {$th->getMessage()}", 1);
130
-        }
131
-
132
-        return $instance;
133
-    }
134
-
135
-    /**
136
-     * Sets a property to the current model
137
-     *
138
-     * @param T $model
139
-     * @param string $property
140
-     * @param mixed $value
141
-     *
142
-     * @return T
143
-     */
144
-    protected function setProperty($model, string $property, $value)
145
-    {
146
-        $property = $this->normaliseProperty($property);
147
-
148
-        // Attempt to set.
149
-        try {
150
-            switch (true) {
151
-                case method_exists($model, $this->generateSetterMethod($property)):
152
-                    $method = $this->generateSetterMethod($property);
153
-                    $model->$method($value);
154
-                    break;
155
-
156
-                case method_exists($model, $this->generateSetterMethod($property, true)):
157
-                    $method = $this->generateSetterMethod($property, true);
158
-                    $model->$method($value);
159
-                    break;
160
-
161
-                default:
162
-                    $model->$property = $value;
163
-                    break;
164
-            }
165
-        } catch (Throwable $th) {
166
-            throw new Exception(sprintf('Failed to set %s of %s model, %s', $property, get_class($model), $th->getMessage()), 1);
167
-        }
168
-
169
-        return $model;
170
-    }
171
-
172
-    /**
173
-     * Normalises a property
174
-     *
175
-     * @param string $property
176
-     *
177
-     * @return string
178
-     */
179
-    protected function normaliseProperty(string $property): string
180
-    {
181
-        return trim(
182
-            preg_replace('/[^a-z0-9]+/', '_', strtolower($property)) ?: ''
183
-        );
184
-    }
185
-
186
-    /**
187
-     * Generates a generic setter method using either underscore [set_property()] or PSR2 style [setProperty()]
188
-     *
189
-     * @param string $property
190
-     * @param bool $underscore
191
-     *
192
-     * @return string
193
-     */
194
-    protected function generateSetterMethod(string $property, bool $underscore = false): string
195
-    {
196
-        return $underscore
197
-            ? "set_{$property}"
198
-            : 'set' . ucfirst($property);
199
-    }
24
+	/**
25
+	 * The model to hydrate
26
+	 *
27
+	 * @var class-string<T>
28
+	 */
29
+	protected $model;
30
+
31
+	/**
32
+	 * The arguments used to create the new instance.
33
+	 *
34
+	 * @var array<string|int, mixed>
35
+	 */
36
+	protected $constructorArgs;
37
+
38
+	/**
39
+	 * @param class-string<T> $model
40
+	 * @param array<string|int, mixed> $constructorArgs
41
+	 */
42
+	public function __construct(string $model = stdClass::class, array $constructorArgs = [])
43
+	{
44
+		$this->model           = $model;
45
+		$this->constructorArgs = $constructorArgs;
46
+	}
47
+
48
+	/**
49
+	 * Map many models
50
+	 *
51
+	 * @param array<int, object|mixed[]> $sources
52
+	 *
53
+	 * @return array<T>
54
+	 */
55
+	public function fromMany(array $sources): array
56
+	{
57
+		return array_map([$this, 'from'], $sources);
58
+	}
59
+
60
+	/**
61
+	 * Map a single model
62
+	 *
63
+	 * @param object|mixed[] $source
64
+	 *
65
+	 * @return T
66
+	 */
67
+	public function from($source)
68
+	{
69
+		switch (true) {
70
+			case is_array($source):
71
+				return $this->fromArray($source);
72
+
73
+			case is_object($source):
74
+				return $this->fromObject($source);
75
+
76
+			default:
77
+				throw new Exception('Models can only be mapped from arrays or objects.', 1);
78
+		}
79
+	}
80
+
81
+	/**
82
+	 * Maps the model from an array of data.
83
+	 *
84
+	 * @param array<string, mixed> $source
85
+	 *
86
+	 * @return T
87
+	 */
88
+	protected function fromArray(array $source)
89
+	{
90
+		$model = $this->newInstance();
91
+		foreach ($source as $key => $value) {
92
+			$this->setProperty($model, $key, $value);
93
+		}
94
+
95
+		return $model;
96
+	}
97
+
98
+	/**
99
+	 * Maps a model from an Object of data
100
+	 *
101
+	 * @param object $source
102
+	 *
103
+	 * @return T
104
+	 */
105
+	protected function fromObject($source)
106
+	{
107
+		$vars = get_object_vars($source);
108
+
109
+		return $this->fromArray($vars);
110
+	}
111
+
112
+	/**
113
+	 * Construct an instance of the model
114
+	 *
115
+	 * @return T
116
+	 */
117
+	protected function newInstance()
118
+	{
119
+		$class = $this->model;
120
+		try {
121
+			/** @var T */
122
+			$instance = empty($this->constructorArgs)
123
+				? new $class()
124
+				: new $class(...$this->constructorArgs);
125
+		} catch (Throwable $th) {
126
+			throw new Exception("Failed to construct model, {$th->getMessage()}", 1);
127
+		}
128
+
129
+		return $instance;
130
+	}
131
+
132
+	/**
133
+	 * Sets a property to the current model
134
+	 *
135
+	 * @param T $model
136
+	 * @param string $property
137
+	 * @param mixed $value
138
+	 *
139
+	 * @return T
140
+	 */
141
+	protected function setProperty($model, string $property, $value)
142
+	{
143
+		$property = $this->normaliseProperty($property);
144
+
145
+		// Attempt to set.
146
+		try {
147
+			switch (true) {
148
+				case method_exists($model, $this->generateSetterMethod($property)):
149
+					$method = $this->generateSetterMethod($property);
150
+					$model->$method($value);
151
+					break;
152
+
153
+				case method_exists($model, $this->generateSetterMethod($property, true)):
154
+					$method = $this->generateSetterMethod($property, true);
155
+					$model->$method($value);
156
+					break;
157
+
158
+				default:
159
+					$model->$property = $value;
160
+					break;
161
+			}
162
+		} catch (Throwable $th) {
163
+			throw new Exception(sprintf('Failed to set %s of %s model, %s', $property, get_class($model), $th->getMessage()), 1);
164
+		}
165
+
166
+		return $model;
167
+	}
168
+
169
+	/**
170
+	 * Normalises a property
171
+	 *
172
+	 * @param string $property
173
+	 *
174
+	 * @return string
175
+	 */
176
+	protected function normaliseProperty(string $property): string
177
+	{
178
+		return trim(
179
+			preg_replace('/[^a-z0-9]+/', '_', strtolower($property)) ?: ''
180
+		);
181
+	}
182
+
183
+	/**
184
+	 * Generates a generic setter method using either underscore [set_property()] or PSR2 style [setProperty()]
185
+	 *
186
+	 * @param string $property
187
+	 * @param bool $underscore
188
+	 *
189
+	 * @return string
190
+	 */
191
+	protected function generateSetterMethod(string $property, bool $underscore = false): string
192
+	{
193
+		return $underscore
194
+			? "set_{$property}"
195
+			: 'set' . ucfirst($property);
196
+	}
200 197
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -195,6 +195,6 @@
 block discarded – undo
195 195
     {
196 196
         return $underscore
197 197
             ? "set_{$property}"
198
-            : 'set' . ucfirst($property);
198
+            : 'set'.ucfirst($property);
199 199
     }
200 200
 }
Please login to merge, or discard this patch.