Completed
Branch master (55ca36)
by Andrey
01:42
created

Core   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 177
Duplicated Lines 9.04 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 0
cbo 8
dl 16
loc 177
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 70 5
A undefined_function() 0 19 3
A undefined_filter() 0 11 1
A enable() 8 8 1
A disable() 8 8 1
A template_include() 0 13 2
A get_search_form() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Rarst\Meadow;
4
use Pimple\Container;
5
6
/**
7
 * Main plugin class.
8
 */
9
class Core extends Container {
10
11
	/**
12
	 * @param array $values
13
	 */
14
	public function __construct( $values = array() ) {
15
16
		global $wp_version;
1 ignored issue
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...
17
18
		$defaults['twig.options']     = array();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
19
		$defaults['twig.directories'] = array();
20
21
		$defaults['twig.loader'] = function ( $meadow ) {
22
23
			// this needs to be lazy or theme switchers and alike explode it
24
25
			$stylesheet_dir  = get_stylesheet_directory();
26
			$template_dir    = get_template_directory();
27
			$calculated_dirs = array(
28
				$stylesheet_dir,
29
				$template_dir,
30
				plugin_dir_path( __DIR__ ) . 'twig',
31
			);
32
33
			// enables explicit inheritance from parent theme in child
34
			if ( $stylesheet_dir !== $template_dir ) {
35
				$calculated_dirs[] = dirname( $template_dir );
36
			}
37
38
			$directories = array_unique(
39
				array_merge(
40
					$calculated_dirs,
41
					$meadow['twig.directories']
42
				)
43
			);
44
45
			return new \Twig_Loader_Filesystem( $directories );
46
		};
47
48
		$defaults['twig.undefined_function'] = array( __CLASS__, 'undefined_function' );
49
		$defaults['twig.undefined_filter']   = array( __CLASS__, 'undefined_filter' );
50
51
		$defaults['twig.environment'] = function ( $meadow ) {
52
			$environment      = new \Twig_Environment( $meadow['twig.loader'], $meadow['twig.options'] );
53
			$meadow_extension = new Extension();
54
			$environment->addExtension( $meadow_extension );
55
			$environment->registerUndefinedFunctionCallback( $meadow['twig.undefined_function'] );
56
			$environment->registerUndefinedFilterCallback( $meadow['twig.undefined_filter'] );
57
58
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
59
				$debug_extension = new \Twig_Extension_Debug();
60
				$environment->addExtension( $debug_extension );
61
				$environment->enableDebug();
62
			}
63
64
			return $environment;
65
		};
66
67
		if ( version_compare( rtrim( $wp_version, '-src' ), '4.7', '>=' ) ) {
68
69
			$defaults['hierarchy'] = function () {
70
				return new Type_Template_Hierarchy();
71
			};
72
		} else {
73
74
			trigger_error( 'Pre–WP 4.7 implementation of Meadow hierarchy is deprecated and will be removed in 1.0.', E_USER_DEPRECATED );
75
76
			$defaults['hierarchy'] = function () {
77
				return new Template_Hierarchy();
78
			};
79
		}
80
81
82
		parent::__construct( array_merge( $defaults, $values ) );
83
	}
84
85
	/**
86
	 * Handler for undefined functions in Twig to pass them through to PHP and buffer echoing versions.
87
	 *
88
	 * @param string $function_name
89
	 *
90
	 * @return bool|\Twig_SimpleFunction
91
	 */
92
	static function undefined_function( $function_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
94
		if ( function_exists( $function_name ) ) {
95
			return new \Twig_SimpleFunction(
96
				$function_name,
97
				function () use ( $function_name ) {
98
99
					ob_start();
100
					$return = call_user_func_array( $function_name, func_get_args() );
101
					$echo   = ob_get_clean();
102
103
					return empty( $echo ) ? $return : $echo;
104
				},
105
				array( 'is_safe' => array( 'all' ) )
106
			);
107
		}
108
109
		return false;
110
	}
111
112
	/**
113
	 * Handler for fallback to WordPress filters for undefined Twig filters in template.
114
	 *
115
	 * @param string $filter_name
116
	 *
117
	 * @return bool|\Twig_SimpleFilter
118
	 */
119
	static function undefined_filter( $filter_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
120
121
		return new \Twig_SimpleFilter(
122
			$filter_name,
123
			function () use ( $filter_name ) {
124
125
				return apply_filters( $filter_name, func_get_arg( 0 ) );
126
			},
127
			array( 'is_safe' => array( 'all' ) )
128
		);
129
	}
130
131 View Code Duplication
	public function enable() {
132
133
		/** @var Template_Hierarchy $hierarchy */
134
		$hierarchy = $this['hierarchy'];
135
		$hierarchy->enable();
136
		add_filter( 'template_include', array( $this, 'template_include' ) );
137
		add_filter( 'get_search_form', array( $this, 'get_search_form' ), 9 );
138
	}
139
140 View Code Duplication
	public function disable() {
141
142
		/** @var Template_Hierarchy $hierarchy */
143
		$hierarchy = $this['hierarchy'];
144
		$hierarchy->disable();
145
		remove_filter( 'template_include', array( $this, 'template_include' ) );
146
		remove_filter( 'get_search_form', array( $this, 'get_search_form' ), 9 );
147
	}
148
149
	/**
150
	 * @param string $template
151
	 *
152
	 * @return string|bool
153
	 */
154
	public function template_include( $template ) {
155
156
		if ( '.twig' === substr( $template, - 5 ) ) {
157
			/** @var \Twig_Environment $twig */
158
			$twig = $this['twig.environment'];
159
160
			echo $twig->render( basename( $template ), apply_filters( 'meadow_context', array() ) );
161
162
			return false;
163
		}
164
165
		return $template;
166
	}
167
168
	/**
169
	 * @param string $form
170
	 *
171
	 * @return string
172
	 */
173
	public function get_search_form( $form ) {
174
175
		// because first time it's action
176
		if ( ! empty( $form ) ) {
177
			/** @var \Twig_Environment $twig */
178
			$twig = $this['twig.environment'];
179
180
			return $twig->render( 'searchform.twig', array() );
181
		}
182
183
		return $form;
184
	}
185
}