TemplateName   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 21 4
A normalize() 0 17 4
A get_as_template() 0 4 1
A get_as_partial() 0 4 1
A get_as_layout() 0 4 1
A with_prefix() 0 21 4
A __construct() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Render;
13
14
use ICanBoogie\Accessor\AccessorTrait;
15
use ICanBoogie\ActiveRecord\Query;
16
17
/**
18
 * Representation of a template name.
19
 *
20
 * @property-read string $as_template Name as template name.
21
 * @property-read string $as_partial Name as partial name.
22
 * @property-read string $as_layout Name as layout name.
23
 */
24
class TemplateName
25
{
26
	use AccessorTrait;
27
28
	const TEMPLATE_PREFIX_VIEW = '';
29
	const TEMPLATE_PREFIX_LAYOUT = '@';
30
	const TEMPLATE_PREFIX_PARTIAL = '_';
31
32
	static private $instances = [];
33
34
	/**
35
	 * @param string|TemplateName $source
36
	 *
37
	 * @return TemplateName
38
	 */
39
	static public function from($source)
40
	{
41
		if ($source instanceof self)
42
		{
43
			return $source;
44
		}
45
46
		if ($source instanceof Query)
0 ignored issues
show
Bug introduced by
The class ICanBoogie\ActiveRecord\Query 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...
47
		{
48
			$source = $source->model->id . '/list';
49
		}
50
51
		$source = static::normalize($source);
52
53
		if (isset(self::$instances[$source]))
54
		{
55
			return self::$instances[$source];
56
		}
57
58
		return self::$instances[$source] = new static($source);
59
	}
60
61
	/**
62
	 * Normalizes a template name by removing any known prefix.
63
	 *
64
	 * @param string $name
65
	 *
66
	 * @return string
67
	 */
68
	static public function normalize($name)
69
	{
70
		$basename = basename($name);
71
		$dirname = $basename != $name ? dirname($name) : null;
72
73
		if (in_array($basename{0}, [ self::TEMPLATE_PREFIX_VIEW, self::TEMPLATE_PREFIX_LAYOUT, self::TEMPLATE_PREFIX_PARTIAL ]))
74
		{
75
			$basename = substr($basename, 1);
76
		}
77
78
		if ($dirname)
79
		{
80
			$basename = $dirname . "/". $basename;
81
		}
82
83
		return $basename;
84
	}
85
86
	private $name;
87
88
	/**
89
	 * Returns the name as template name.
90
	 *
91
	 * @return string
92
	 */
93
	protected function get_as_template()
94
	{
95
		return $this->name;
96
	}
97
98
	/**
99
	 * Returns the name as partial name.
100
	 *
101
	 * @return string
102
	 */
103
	protected function get_as_partial()
104
	{
105
		return $this->with_prefix(self::TEMPLATE_PREFIX_PARTIAL);
106
	}
107
108
	/**
109
	 * Returns the name as layout name.
110
	 *
111
	 * @return string
112
	 */
113
	protected function get_as_layout()
114
	{
115
		return $this->with_prefix(self::TEMPLATE_PREFIX_LAYOUT);
116
	}
117
118
	/**
119
	 * Returns the template name with the specified prefix.
120
	 *
121
	 * @param string $prefix
122
	 *
123
	 * @return string
124
	 */
125
	public function with_prefix($prefix)
126
	{
127
		$name = $this->name;
128
129
		if (!$prefix)
130
		{
131
			return $name;
132
		}
133
134
		$basename = basename($name);
135
		$dirname = $basename != $name ? dirname($name) : null;
136
137
		$name = $prefix . $basename;
138
139
		if ($dirname)
140
		{
141
			$name = $dirname . "/" . $name;
142
		}
143
144
		return $name;
145
	}
146
147
	/**
148
	 * Initializes the {@link $name} property.
149
	 *
150
	 * @param string $name
151
	 */
152
	protected function __construct($name)
153
	{
154
		$this->name = (string) $name;
155
	}
156
157
	/**
158
	 * Returns the {@link $name} property.
159
	 *
160
	 * @return string
161
	 */
162
	public function __toString()
163
	{
164
		return $this->name;
165
	}
166
}
167