Completed
Push — main ( 169704 )
by
unknown
24s queued 19s
created

addCharsetMeta()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 14
c 2
b 0
f 0
nc 7
nop 2
dl 0
loc 20
rs 6.9666

How to fix   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
3
/**
4
 * TPageGlobalizationCharsetBehavior class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util\Behaviors;
12
13
use Prado\Prado;
14
use Prado\TPropertyValue;
15
use Prado\Web\UI\WebControls\TMetaTag;
16
17
/**
18
 * TPageGlobalizationCharsetBehavior class.
19
 *
20
 * TPageGlobalizationCharsetBehavior attaches to pages and adds charset
21
 * meta to the head from globalization.  If there is no globalization,
22
 * the default charset is used, 'utf-8'.
23
 * @author Brad Anderson <[email protected]>
24
 * @since 4.2.0
25
 */
26
class TPageGlobalizationCharsetBehavior extends \Prado\Util\TBehavior
27
{
28
	/** @var bool check the existing meta tags for the charset before adding it */
29
	private $_checkMetaCharset = false;
30
31
	/**
32
	 * This handles the TPage.OnInitComplete event to place no-cache
33
	 * meta in the head.
34
	 * @return array of events as keys and methods as values
35
	 */
36
	public function events()
37
	{
38
		return ['OnInitComplete' => 'addCharsetMeta'];
39
	}
40
41
	/**
42
	 * This method places no-cache meta in the head.
43
	 * @param object $page object raising the event
44
	 * @param mixed $param the parameter of the raised event
45
	 */
46
	public function addCharsetMeta($page, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

46
	public function addCharsetMeta($page, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
	{
48
		if ($this->getEnabled() && ($head = $page->getHead())) {
49
			$hasCharset = false;
50
			$metatags = $head->getMetaTags();
51
			if ($this->_checkMetaCharset) {
52
				foreach ($metatags as $meta) {
53
					if (empty($meta->getHttpEquiv()) && empty($meta->getContent()) && empty($meta->getName()) && empty($meta->getScheme()) && !empty($meta->getCharset())) {
54
						$hasCharset = true;
55
					}
56
				}
57
			}
58
			if (!$hasCharset) {
59
				$charset = 'utf-8';
60
				if ($globalization = Prado::getApplication()->getGlobalization()) {
61
					$charset = $globalization->getCharset();
62
				}
63
				$meta = new TMetaTag();
64
				$meta->setCharset($charset);
65
				$metatags->add($meta);
66
			}
67
		}
68
	}
69
70
	/**
71
	 * @return bool checks existing meta tags for no cache
72
	 */
73
	public function getCheckMetaCharset()
74
	{
75
		return $this->_checkMetaCharset;
76
	}
77
78
	/**
79
	 * @param bool $value checks existing meta tags for no cache
80
	 */
81
	public function setCheckMetaCharset($value)
82
	{
83
		$this->_checkMetaCharset = TPropertyValue::ensureBoolean($value);
84
	}
85
}
86