Passed
Pull Request — master (#64)
by Glynn
06:00 queued 02:22
created
src/Registrar/Meta_Data_Registrar.php 1 patch
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -37,8 +37,8 @@  discard block
 block discarded – undo
37 37
 	 * @return bool
38 38
 	 * @throws \Exception if fails to register meta data.
39 39
 	 */
40
-	public function register_for_post_type( Meta_Data $meta, string $post_type ):bool {
41
-		return $this->register_meta( $meta, 'post', $post_type );
40
+	public function register_for_post_type(Meta_Data $meta, string $post_type):bool {
41
+		return $this->register_meta($meta, 'post', $post_type);
42 42
 	}
43 43
 
44 44
 	/**
@@ -49,8 +49,8 @@  discard block
 block discarded – undo
49 49
 	 * @return bool
50 50
 	 * @throws \Exception if fails to register meta data.
51 51
 	 */
52
-	public function register_for_term( Meta_Data $meta, string $taxonomy ):bool {
53
-		return $this->register_meta( $meta, 'term', $taxonomy );
52
+	public function register_for_term(Meta_Data $meta, string $taxonomy):bool {
53
+		return $this->register_meta($meta, 'term', $taxonomy);
54 54
 	}
55 55
 
56 56
 	/**
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
 	 * @return bool
61 61
 	 * @throws \Exception if fails to register meta data.
62 62
 	 */
63
-	public function register_for_user( Meta_Data $meta ): bool {
64
-		return $this->register_meta( $meta, 'user', '' );
63
+	public function register_for_user(Meta_Data $meta): bool {
64
+		return $this->register_meta($meta, 'user', '');
65 65
 	}
66 66
 
67 67
 	/**
@@ -71,8 +71,8 @@  discard block
 block discarded – undo
71 71
 	 * @return bool
72 72
 	 * @throws \Exception if fails to register meta data.
73 73
 	 */
74
-	public function register_for_comment( Meta_Data $meta ): bool {
75
-		return $this->register_meta( $meta, 'comment', '' );
74
+	public function register_for_comment(Meta_Data $meta): bool {
75
+		return $this->register_meta($meta, 'comment', '');
76 76
 	}
77 77
 
78 78
 	/**
@@ -86,25 +86,25 @@  discard block
 block discarded – undo
86 86
 	 * @return bool
87 87
 	 * @throws \Exception if fails to register meta data.
88 88
 	 */
89
-	protected function register_meta( Meta_Data $meta, string $meta_type, string $sub_type ): bool {
89
+	protected function register_meta(Meta_Data $meta, string $meta_type, string $sub_type): bool {
90 90
 		// Clone and set the post type, while enforcing it as a post meta.
91 91
 		$meta = clone $meta;
92
-		$meta->object_subtype( $sub_type );
93
-		$meta->meta_type( $meta_type );
92
+		$meta->object_subtype($sub_type);
93
+		$meta->meta_type($meta_type);
94 94
 
95 95
 		// Normalise rest schema model to array.
96
-		$meta = $this->normalise_rest_schema( $meta );
96
+		$meta = $this->normalise_rest_schema($meta);
97 97
 
98
-		$result = register_meta( $meta->get_meta_type(), $meta->get_meta_key(), $meta->parse_args() );
99
-		if ( ! $result ) {
98
+		$result = register_meta($meta->get_meta_type(), $meta->get_meta_key(), $meta->parse_args());
99
+		if ( ! $result) {
100 100
 			throw new \Exception(
101 101
 				"Failed to register {$meta->get_meta_key()} (meta) for {$sub_type} of {$meta_type} type"
102 102
 			);
103 103
 		}
104 104
 
105 105
 		// Maybe register rest fields.
106
-		if ( false !== $meta->get_rest_schema() ) {
107
-			$this->register_meta_rest_field( $meta );
106
+		if (false !== $meta->get_rest_schema()) {
107
+			$this->register_meta_rest_field($meta);
108 108
 		}
109 109
 
110 110
 		return $result;
@@ -119,11 +119,11 @@  discard block
 block discarded – undo
119 119
 	 * @param \PinkCrab\Registerables\Meta_Data $meta
120 120
 	 * @return \PinkCrab\Registerables\Meta_Data
121 121
 	 */
122
-	protected function normalise_rest_schema( Meta_Data $meta ): Meta_Data {
123
-		if ( \class_exists( 'PinkCrab\WP_Rest_Schema\Argument\Argument' )
122
+	protected function normalise_rest_schema(Meta_Data $meta): Meta_Data {
123
+		if (\class_exists('PinkCrab\WP_Rest_Schema\Argument\Argument')
124 124
 		&& $meta->get_rest_schema() instanceof \PinkCrab\WP_Rest_Schema\Argument\Argument
125 125
 		) {
126
-			$meta->rest_schema( \PinkCrab\WP_Rest_Schema\Parser\Argument_Parser::for_meta_data( $meta->get_rest_schema() ) );
126
+			$meta->rest_schema(\PinkCrab\WP_Rest_Schema\Parser\Argument_Parser::for_meta_data($meta->get_rest_schema()));
127 127
 		}
128 128
 		return $meta;
129 129
 	}
@@ -134,22 +134,22 @@  discard block
 block discarded – undo
134 134
 	* @param \PinkCrab\Registerables\Meta_Data $meta
135 135
 	* @return void
136 136
 	*/
137
-	public function register_meta_rest_field( Meta_Data $meta ) {
137
+	public function register_meta_rest_field(Meta_Data $meta) {
138 138
 		// Skip if not sub type defined for post or term.
139
-		if ( null === $meta->get_subtype() ) {
139
+		if (null === $meta->get_subtype()) {
140 140
 			return;
141 141
 		}
142 142
 
143 143
 		add_action(
144 144
 			'rest_api_init',
145
-			function () use ( $meta ) {
145
+			function() use ($meta) {
146 146
 				register_rest_field(
147 147
 					$meta->get_subtype(),
148 148
 					$meta->get_meta_key(),
149 149
 					array( // @phpstan-ignore-line WP Docblock doesn't give enough details of callable param types, so throws false positive
150
-						'get_callback'    => $meta->get_rest_view() ?? $this->create_rest_get_method( $meta ),
150
+						'get_callback'    => $meta->get_rest_view() ?? $this->create_rest_get_method($meta),
151 151
 						'schema'          => $meta->get_rest_schema(),
152
-						'update_callback' => $meta->get_rest_update() ?? $this->create_rest_update_method( $meta ),
152
+						'update_callback' => $meta->get_rest_update() ?? $this->create_rest_update_method($meta),
153 153
 					)
154 154
 				);
155 155
 			}
@@ -162,23 +162,23 @@  discard block
 block discarded – undo
162 162
 	 * @param \PinkCrab\Registerables\Meta_Data $meta
163 163
 	 * @return callable(array<mixed>):void
164 164
 	 */
165
-	protected function create_rest_get_method( Meta_Data $meta ): callable {
166
-		return function( $model ) use ( $meta ) {
167
-			switch ( $meta->get_meta_type() ) {
165
+	protected function create_rest_get_method(Meta_Data $meta): callable {
166
+		return function($model) use ($meta) {
167
+			switch ($meta->get_meta_type()) {
168 168
 				case 'post':
169
-					$value = get_post_meta( $model['id'], $meta->get_meta_key(), true );
169
+					$value = get_post_meta($model['id'], $meta->get_meta_key(), true);
170 170
 					break;
171 171
 
172 172
 				case 'term':
173
-					$value = get_term_meta( $model['id'], $meta->get_meta_key(), true );
173
+					$value = get_term_meta($model['id'], $meta->get_meta_key(), true);
174 174
 					break;
175 175
 
176 176
 				case 'user':
177
-					$value = get_user_meta( $model['id'], $meta->get_meta_key(), true );
177
+					$value = get_user_meta($model['id'], $meta->get_meta_key(), true);
178 178
 					break;
179 179
 
180 180
 				case 'comment':
181
-					$value = get_comment_meta( $model['id'], $meta->get_meta_key(), true );
181
+					$value = get_comment_meta($model['id'], $meta->get_meta_key(), true);
182 182
 					break;
183 183
 
184 184
 				default:
@@ -196,31 +196,31 @@  discard block
 block discarded – undo
196 196
 	 * @param Meta_Data $meta
197 197
 	 * @return \Closure(mixed, \WP_Post|\WP_Term|\WP_User|\WP_Comment): mixed
198 198
 	 */
199
-	protected function create_rest_update_method( Meta_Data $meta ): \Closure {
199
+	protected function create_rest_update_method(Meta_Data $meta): \Closure {
200 200
 		/**
201 201
 		 * @param mixed $value
202 202
 		 * @param \WP_Post|\WP_Term|\WP_User|\WP_Comment $object
203 203
 		 */
204
-		return function( $value, $object ) use ( $meta ) {
205
-			switch ( $meta->get_meta_type() ) {
204
+		return function($value, $object) use ($meta) {
205
+			switch ($meta->get_meta_type()) {
206 206
 				case 'post':
207 207
 					/** @var \WP_Post $object */
208
-					update_post_meta( $object->ID, $meta->get_meta_key(), $value );
208
+					update_post_meta($object->ID, $meta->get_meta_key(), $value);
209 209
 					break;
210 210
 
211 211
 				case 'term':
212 212
 					/** @var \WP_Term $object */
213
-					update_term_meta( $object->term_id, $meta->get_meta_key(), $value );
213
+					update_term_meta($object->term_id, $meta->get_meta_key(), $value);
214 214
 					break;
215 215
 
216 216
 				case 'user':
217 217
 					/** @var \WP_User $object */
218
-					update_user_meta( $object->ID, $meta->get_meta_key(), $value );
218
+					update_user_meta($object->ID, $meta->get_meta_key(), $value);
219 219
 					break;
220 220
 
221 221
 				case 'comment':
222 222
 					/** @var \WP_Comment $object */
223
-					update_comment_meta( (int) $object->comment_ID, $meta->get_meta_key(), $value );
223
+					update_comment_meta((int) $object->comment_ID, $meta->get_meta_key(), $value);
224 224
 					break;
225 225
 
226 226
 				default:
Please login to merge, or discard this patch.