Completed
Push — develop ( a61784...aed985 )
by Zack
27:19 queued 14:49
created

Renderer::maybe_print_notices()   C

Complexity

Conditions 10
Paths 73

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 10.1283

Importance

Changes 0
Metric Value
cc 10
nc 73
nop 1
dl 0
loc 86
ccs 41
cts 46
cp 0.8913
crap 10.1283
rs 6.4387
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The \GV\Renderer class.
11
 *
12
 * The base for all renderers.
13
 */
14
class Renderer {
15
	/**
16
	 * Initialization.
17
	 */
18 102
	public function __construct() {
19 102
		if ( ! has_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) ) ) {
20 31
			add_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) );
21
		}
22 102
	}
23
24
	/**
25
	 * Print unconfigured notices to admins.
26
	 * Print reserved slug warnings.
27
	 *
28
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
29
	 *
30
	 * @return void
31
	 */
32 35
	public static function maybe_print_notices( $gravityview = null ) {
33 35
		if ( ! $gravityview instanceof \GV\Template_Context ) {
34
			/** Call the legacy code. */
35
			\GravityView_frontend::getInstance()->context_not_configured_warning( gravityview_get_view_id() );
36
			return;
37
		}
38
39
		/**
40
		 * Check reserved slugs.
41
		 */
42 35
		global $wp;
43 35
		global $wp_rewrite;
44
45
		$reserved_slugs = array(
46 35
			$wp_rewrite->search_base,
47 35
			apply_filters( 'gravityview_directory_endpoint', 'entry' ),
48
		);
49
50 35
		$post_types = get_post_types();
51
52 35
		foreach( $post_types as $post_type ) {
53 35
			$post_type_rewrite = get_post_type_object( $post_type )->rewrite;
54
55 35
			if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
56 35
				$reserved_slugs[] = $slug;
57
			}
58
		}
59
60 35
		unset( $post_types, $post_type_rewrite );
61
62
		/**
63
		 * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
64
		 * @since 2.5
65
		 * @param[in,out] array $reserved_slugs An array of strings, reserved slugs.
66
		 * @param \GV\Template_Context $gravityview The context.
67
		 */
68 35
		$reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
69
70 35
		$reserved_slugs = array_map( 'strtolower', $reserved_slugs );
71
72 35
		if ( in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
73 1
			gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
74
75 1
			$title   = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gravityview' );
76 1
			$message = __( 'Please <a href="%s">read this article</a> for more information.', 'gravityview' );
77 1
			$message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
78
79 1
			$output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
80
81 1
			echo \GVCommon::generate_notice( $output, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
		}
83
84
		/**
85
		 * Check empty configuration.
86
		 */
87
		switch ( true ) {
88 35
			case ( $gravityview->request->is_edit_entry() ):
89
				$tab = __( 'Edit Entry', 'gravityview' );
90
				$context = 'edit';
91
				break;
92 35
			case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
93 10
				$tab = __( 'Single Entry', 'gravityview' );
94 10
				$context = 'single';
95 10
				break;
96
			default:
97 27
				$tab = __( 'Multiple Entries', 'gravityview' );
98 27
				$context = 'directory';
99 27
				break;
100
		}
101
102 35
		$cls = $gravityview->template;
103 35
		$slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
104 35
		if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
105 28
			return;
106
		}
107
108 7
		$title = sprintf( esc_html_x( 'The %s layout has not been configured.', 'Displayed when a View is not configured. %s is replaced by the tab label', 'gravityview' ), $tab );
109 7
		$edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $gravityview->view->ID, $context ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110 7
		$action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
111 7
		$message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
112
113 7
		$image =  sprintf( '<img alt="%s" src="%s" style="margin-top: 10px;" />', $tab, esc_url( plugins_url( sprintf( 'assets/images/tab-%s.png', $context ), GRAVITYVIEW_FILE ) ) );
114 7
		$output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
115
116 7
		echo \GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117 7
	}
118
119
	/**
120
	 * Warn about legacy template being used.
121
	 *
122
	 * Generate a callback that shows which legacy template was at fault.
123
	 * Used in gravityview_before.
124
	 *
125
	 * @param \GV\View $view The view we're looking at.
126
	 * @param string $path The path of the offending template.
127
	 *
128
	 * @return \Callable A closure used in the filter.
129
	 */
130
	public function legacy_template_warning( $view, $path ) {
131
		return function() use ( $view, $path ) {
132
			// Do not panic for now...
133
		};
134
	}
135
}
136