CachedService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 6 1
A resolveBaseService() 0 8 1
A translateBaseService() 0 7 1
1
<?php
2
3
namespace BrightComponents\Services\Traits;
4
5
use Illuminate\Support\Facades\Config;
6
7
trait CachedService
8
{
9
    /**
10
     * Respond to service call.
11
     *
12
     * @param  array  $parameters
13
     *
14
     * @return mixed
15
     */
16
    public static function call(...$parameters)
17
    {
18
        $decorator = new static(static::resolveBaseService());
0 ignored issues
show
Unused Code introduced by
The call to CachedService::__construct() has too many arguments starting with static::resolveBaseService().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
19
20
        return $decorator->cache(...$parameters);
0 ignored issues
show
Bug introduced by
It seems like cache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
21
    }
22
23
    /**
24
     * Resolve the base service for the cached service.
25
     *
26
     * @return string
27
     */
28
    private static function resolveBaseService()
29
    {
30
        $cachedServices = Config::get('service-classes.cached_services.classes', null);
31
32
        $baseService = $cachedServices[static::class] ?? static::translateBaseService();
33
34
        return resolve($baseService);
35
    }
36
37
    /**
38
     * Translate the cached service name to the base service name.
39
     *
40
     * @return string
41
     */
42
    private static function translateBaseService()
43
    {
44
        $cachedServicePrefix = Config::get('service-classes.cached_services.prefix');
45
        $cachedServiceNamespace = '\\'.Config::get('service-classes.cached_services.namespace');
46
47
        return str_replace([$cachedServicePrefix, $cachedServiceNamespace], ['', ''], static::class);
48
    }
49
}
50