Passed
Push — master ( 510e0e...2d2242 )
by Paul
02:20
created

PostMeta::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\Application;
6
7
class PostMeta
8
{
9
	public function get( $metaKey, array $args = [] )
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
10
	{
11
		if( empty( $metaKey ))return;
12
13
		$args = $this->normalize( $args );
14
		$metaKey = $this->buildMetaKey( $metaKey, $args['prefix'] );
15
		$metaValue = get_post_meta( $args['id'], $metaKey, $args['single'] );
16
17
		if( is_string( $metaValue )) {
18
			$metaValue = trim( $metaValue );
19
		}
20
		return empty( $metaValue )
21
			? $args['fallback']
22
			: $metaValue;
23
	}
24
25
	protected function buildMetaKey( $metaKey, $prefix )
26
	{
27
		return ( substr( $metaKey, 0, 1 ) == '_' && !empty( $prefix ))
28
			? sprintf( '_%s%s', rtrim( $prefix, '_' ), $metaKey )
29
			: $prefix . $metaKey;
30
	}
31
32
	protected function normalize( array $args )
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
33
	{
34
		$defaults = [
35
			'id'       => get_the_ID(),
36
			'fallback' => '',
37
			'single'   => true,
38
			'prefix'   => Application::PREFIX,
39
		];
40
		return shortcode_atts( $defaults, array_change_key_case( $args ));
41
	}
42
}
43