Completed
Push — develop ( 9aa6f3...652775 )
by Paul
02:20
created

PostMeta::get()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 38.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
class PostMeta
6
{
7
	public function get( $metaKey, array $args = [] )
8
	{
9
		if( empty( $metaKey ))return;
10
11
		$args = $this->normalize( $args );
12
		$metaKey = $this->buildMetaKey( $metaKey, $args['prefix'] );
13
		$metaValue = get_post_meta( $args['ID'], $metaKey, $args['single'] );
14
15
		return empty( $metaValue )
16
			? $args['fallback']
17
			: $metaValue;
18
	}
19
20
	protected function buildMetaKey( $metaKey, $prefix )
21
	{
22
		return ( substr( $metaKey, 0, 1 ) == '_' && !empty( $prefix ))
23
			? sprintf( '_%s%s', rtrim( $prefix, '_' ), $metaKey )
24
			: $prefix . $metaKey;
25
		}
26
	}
27
28
	protected function normalize( array $args )
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PROTECTED
Loading history...
29
	{
30
		$defaults = [
31
			'ID'       => get_the_ID(),
32
			'fallback' => '',
33
			'single'   => true,
34
			'prefix'   => apply_filters( 'castor/postmeta/prefix', 'pollux_' ),
35
		];
36
		return shortcode_atts( $defaults, $args );
37
	}
38
}
39