SchemaOrg   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 49
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 7 1
A initSchemaOrg() 0 11 4
A schemaOrgScript() 0 9 2
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
7
use Riclep\Storyblok\Page;
8
9
trait SchemaOrg
10
{
11
	/**
12
	 * Automatically called to add a schema to the Page
13
	 */
14
	protected function initSchemaOrg(): void
15
	{
16
		if (method_exists($this, 'schemaOrg')) {
17
			if ($this instanceof Page) {
18
				$page = $this;
19
			} else {
20
				$page = $this->page();
0 ignored issues
show
Bug introduced by
It seems like page() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

20
				/** @scrutinizer ignore-call */ 
21
    $page = $this->page();
Loading history...
21
			}
22
23
			if ($page) {
24
				$this->add($page);
25
			}
26
		}
27
28
	}
29
30
	/**
31
	 * Returns the JavaScript JSON-LD string
32
	 *
33
	 * @return string
34
	 */
35
	public function schemaOrgScript(): string
36
	{
37
		$schemaJson = '';
38
39
		foreach ($this->meta()['schema_org'] as $schema) {
0 ignored issues
show
Bug introduced by
It seems like meta() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
		foreach ($this->/** @scrutinizer ignore-call */ meta()['schema_org'] as $schema) {
Loading history...
40
			$schemaJson .= $schema->toScript();
41
		}
42
43
		return $schemaJson;
44
	}
45
46
	/**
47
	 * Adds the schema to the meta of the current page
48
	 *
49
	 * @param $page
50
	 */
51
	private function add($page): void
52
	{
53
		$currentSchemaOrg = $page->meta('schema_org');
54
55
		$currentSchemaOrg[] = $this->schemaOrg();
0 ignored issues
show
Bug introduced by
The method schemaOrg() does not exist on Riclep\Storyblok\Traits\SchemaOrg. Did you maybe mean schemaOrgScript()? ( Ignorable by Annotation )

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

55
		/** @scrutinizer ignore-call */ 
56
  $currentSchemaOrg[] = $this->schemaOrg();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
57
		$page->replaceMeta('schema_org', $currentSchemaOrg ?? []);
58
	}
59
}