Completed
Push — develop ( 811f38...1e789a )
by Paul
04:11
created

Theme::getPageTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
use GeminiLabs\Castor\Helpers\PostMeta;
6
7
class Theme
8
{
9
	public $postmeta;
10
11
	public function __construct( PostMeta $postmeta )
12
	{
13
		$this->postmeta = $postmeta;
14
	}
15
16
	/**
17
	 * @param string $asset
18
	 *
19
	 * @return string
20
	 */
21
	public function assetPath( $asset )
22
	{
23
		return $this->paths( 'dir.stylesheet' ) . 'assets/' . $asset;
24
	}
25
26
	/**
27
	 * @param string $asset
28
	 *
29
	 * @return string
30
	 */
31
	public function assetUri( $asset )
32
	{
33
		return $this->paths( 'uri.stylesheet' ) . 'assets/' . $asset;
34
	}
35
36
	/**
37
	 * @return bool
38
	 */
39
	public function displaySidebar()
40
	{
41
		$conditions = [
42
			is_archive(),
43
			is_home(),
44
			is_single(),
45
		];
46
47
		$display = in_array( true, $conditions );
48
49
		return apply_filters( 'castor/display/sidebar', $display );
50
	}
51
52
	/**
53
	 * @param string $asset
54
	 *
55
	 * @return string
56
	 */
57
	public function imagePath( $asset )
58
	{
59
		return $this->paths( 'dir.stylesheet' ) . 'assets/img/' . $asset;
60
	}
61
62
	/**
63
	 * @param string $asset
64
	 *
65
	 * @return string
66
	 */
67
	public function imageUri( $asset )
68
	{
69
		return $this->paths( 'uri.stylesheet' ) . 'assets/img/' . $asset;
70
	}
71
72
	public function pageTitle()
73
	{
74
		foreach( ['is_404', 'is_archive', 'is_home', 'is_page', 'is_search'] as $bool ) {
75
			if( !$bool() )continue;
76
			$method = sprintf( 'get%sTitle', ucfirst( str_replace( 'is_', '', $bool )));
77
			return $this->$method();
78
		}
79
80
		return get_the_title();
81
	}
82
83
	/**
84
	 * @param null|string $path
85
	 *
86
	 * @return array|string
87
	 */
88
	public function paths( $path = null )
89
	{
90
		$paths = [
91
			'dir.stylesheet' => get_stylesheet_directory(),
92
			'dir.template'   => get_template_directory(),
93
			'dir.upload'     => wp_upload_dir()['basedir'],
94
			'uri.stylesheet' => get_stylesheet_directory_uri(),
95
			'uri.template'   => get_template_directory_uri(),
96
		];
97
98
		if( is_null( $path )) {
99
			return $paths;
100
		}
101
102
		return array_key_exists( $path, $paths )
103
			? trailingslashit( $paths[$path] )
104
			: '';
105
	}
106
107
	protected function get404Title()
108
	{
109
		return __( 'Not Found', 'castor' );
110
	}
111
112
	protected function getArchiveTitle()
113
	{
114
		return get_the_archive_title();
115
	}
116
117
	protected function getHomeTitle()
118
	{
119
		return ( $home = get_option( 'page_for_posts', true ))
120
			? get_the_title( $home )
121
			: __( 'Latest Posts', 'castor' );
122
	}
123
124
	protected function getPageTitle()
125
	{
126
		return $title = $this->postmeta->get( 'title' )
0 ignored issues
show
Unused Code introduced by
$title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
			? $title
0 ignored issues
show
Bug introduced by
The variable $title seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
128
			: get_the_title();
129
	}
130
131
	protected function getSearchTitle()
132
	{
133
		return sprintf( __( 'Search Results for %s', 'castor' ), get_search_query() );
134
	}
135
}
136