HasChildClasses::getChildClassName()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 3
b 0
f 0
nc 7
nop 2
dl 0
loc 15
rs 9.6111
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
10
trait HasChildClasses
11
{
12
	/**
13
	 * A general method for finding child classes such as nested Blocks and Fields.
14
	 * Tries to match them based on their type and name
15
	 *
16
	 * @param $type
17
	 * @param $name
18
	 * @return string|null
19
	 */
20
	public function getChildClassName($type, $name): ?string
21
	{
22
		foreach (Arr::wrap(config('storyblok.component_class_namespace')) as $namespace) {
23
			if (class_exists($namespace . Str::pluralStudly($type) . '\\' . Str::studly($name))) {
24
				return $namespace . Str::pluralStudly($type) . '\\' . Str::studly($name);
25
			}
26
		}
27
28
		foreach (Arr::wrap(config('storyblok.component_class_namespace')) as $namespace) {
29
			if (class_exists($namespace . Str::studly($type))) {
30
				return $namespace . Str::studly($type);
31
			}
32
		}
33
34
		return null;
35
	}
36
}