Completed
Push — develop ( 828be0...07d5bb )
by Gennady
18:27
created

views::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
namespace GV\Wrappers;
3
4
/**
5
 * This file contains magic wrapper code for `gravityview()`.
6
 *
7
 * Every `gravityview()` magic key maps to a class that exposes more magic.
8
 *  Chains of inifite magic can be constructed spanning seas of mermaids,
9
 *  valleys of unicorns and whirlwinds of shooting stars.
10
 */
11
12
/**
13
 * The views magic wrapper.
14
 */
15
class views {
0 ignored issues
show
Coding Style introduced by
Class name "views" is not in camel caps format
Loading history...
16
17
	/**
18
	 * @var \GV\View An internal View keeper.
19
	 */
20
	private $view = null;
21
22
	/**
23
	 * Gets a View.
24
	 *
25
	 * Doesn't care what you provide it. Will try to find
26
	 *  out what you need from the current context, from the supplied
27 6
	 *  args, etc.
28
	 *
29
	 * @param string|int|array|\GV\View\WP_Post|null Anything goes.
30
	 *
31
	 * @return \GV\View|null The detected View.
32 6
	 */
33 1
	public function get( $view = null ) {
34
35
		/**
36
		 * By View.
37
		 */
38
		if ( $view instanceof \GV\View && $view->ID ) {
39 6
			return $this->get( $view->ID );
40 1
		}
41
42
		/** 
43
		 * By View ID.
44
		 */
45
		if ( is_numeric( $view ) ) {
46 6
			return \GV\View::by_id( $view );
47 1
		}
48
49
		/** 
50
		 * By post object.
51
		 */
52
		if ( $view instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53 6
			return \GV\View::from_post( $view );
54 1
		}
55
56
		/**
57
		 * By array.
58
		 */
59
		if ( is_array( $view ) && ! empty( $view['id'] ) ) {
60 6
			return $this->get( $view['id'] );
61 6
		}
62 5
63
		/**
64
		 * From various contexts.
65 1
		 */
66
		if ( is_null( $view ) ) {
67 1
			if ( in_array( get_class( gravityview()->request ), array( 'GV\Frontend_Request', 'GV\Mock_Request' ) ) && $view = gravityview()->request->is_view() ) {
68
				return $view;
69
			}
70
71 1
			global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
72
73
			if ( $post instanceof \WP_Post && $post->post_type == 'gravityview' ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
74
				return $this->get( $post );
75
			}
76
		}
77
	}
78
79
	/**
80
	 * Mock the internal pointer.
81
	 *
82
	 * @param \GV\View $view The View to supply on fallback in ::get()
83
	 *
84
	 * @return void
85
	 */
86
	public function set( $view ) {
87
		$this->view = $view;
88
	}
89
}
90