Completed
Push — develop ( 0f7a5c...505371 )
by Paul
02:14
created

Media::validateArgs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 4
nop 2
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
use GeminiLabs\Castor\Gallery;
6
use GeminiLabs\Castor\Image;
7
use GeminiLabs\Castor\Video;
8
use BadMethodCallException;
9
10
/**
11
 * @method string      gallery( array $args )
12
 * @method \WP_Query   getGallery( array $args )
13
 * @method object|void getImage( int|string $attachment )
14
 * @method object|void getVideo( string|array $args )
15
 * @method string|void image( int|string $attachment, string|array $size )
16
 * @method string|void video( string|array $args )
17
 */
18
class Media
19
{
20
	protected $gallery;
21
	protected $image;
22
	protected $video;
23
24
	public function __construct( Gallery $gallery, Image $image, Video $video )
25
	{
26
		$this->gallery = $gallery;
27
		$this->image   = $image;
28
		$this->video   = $video;
29
	}
30
31
	/**
32
	 * @param string $name
33
	 * @return string|void
34
	 * @throws BadMethodCallException
35
	 */
36
	public function __call( $name, array $args )
37
	{
38
		$mediaType = $this->validateMethod( $name );
39
		$args = $this->validateArgs( $args, $mediaType );
0 ignored issues
show
Security Bug introduced by
It seems like $mediaType defined by $this->validateMethod($name) on line 38 can also be of type false; however, GeminiLabs\Castor\Helpers\Media::validateArgs() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
40
41
		if( str_replace( $mediaType, '', strtolower( $name ))) {
42
			return $this->$mediaType->get( $args[0] )->$mediaType;
43
		}
44
		return !empty( $args[1] )
45
			? $this->$mediaType->get( $args[0] )->render( $args[1] )
46
			: $this->$mediaType->get( $args[0] )->render();
47
	}
48
49
	/**
50
	 * @param string $name
51
	 * @param mixed $args
52
	 * @return mixed
53
	 * @throws BadMethodCallException
54
	 */
55
	public function get( $name, $args = [] )
56
	{
57
		$mediaType = $this->validateMethod( $name );
58
		return $this->$mediaType->get( $args )->$mediaType;
59
	}
60
61
	/**
62
	 * @param string $name
63
	 * @return array
64
	 * @throws BadMethodCallException
65
	 */
66
	protected function validateArgs( array $args, $name )
67
	{
68
		if( !count( $args ) && $name == 'image' ) {
69
			$args[] = get_post_thumbnail_id();
70
		}
71
		if( count( $args )) {
72
			return $args;
73
		}
74
		throw new BadMethodCallException( sprintf( 'Missing arguments for: %s', $name ));
75
	}
76
77
	/**
78
	 * @param string $name
79
	 * @return string|false
80
	 * @throws BadMethodCallException
81
	 */
82
	protected function validateMethod( $name )
83
	{
84
		foreach( [$name, strtolower( substr( $name, 3 ))] as $method ) {
85
			if( property_exists( $this, $method ) && is_object( $this->$method )) {
86
				return $method;
87
			}
88
		}
89
		throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
90
	}
91
}
92