TerminateTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 55
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sum() 0 4 1
A product() 0 4 1
A reduce() 0 14 2
A join() 0 5 1
A count() 0 4 1
1
<?php
2
namespace Spindle\Collection\Traits;
3
4
trait TerminateTrait
5
{
6
    /**
7
     * @return string
8
     */
9 1
    public function join($separator)
10
    {
11 1
        $arr = $this->toArray();
0 ignored issues
show
Bug introduced by
It seems like toArray() 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...
12 1
        return implode($separator, $arr);
13
    }
14
15
    /**
16
     * @return int|float
17
     */
18 5
    public function sum()
19
    {
20 5
        return $this->reduce('$_carry + $_', 0);
21
    }
22
23
    /**
24
     * @return int|float
25
     */
26 1
    public function product()
27
    {
28 1
        return $this->reduce('$_carry * $_', 1);
29
    }
30
31
    /**
32
     * @return int
33
     */
34 1
    public function count()
35
    {
36 1
        return $this->reduce('$_carry + 1', 0);
37
    }
38
39
    /**
40
     * @param string|callable $fn '$_carry + $_'
41
     * @param mixed $initial
42
     * @return mixed
43
     */
44 8
    public function reduce($fn, $initial = null)
45
    {
46 8
        $ops = $this->ops;
0 ignored issues
show
Bug introduced by
The property ops does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47 8
        $this->vars['_carry'] = $initial;
0 ignored issues
show
Bug introduced by
The property vars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48 8
        if (is_callable($fn)) {
49 1
            $fn_name = '_fn' . $this->fn_cnt++;
0 ignored issues
show
Bug introduced by
The property fn_cnt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50 1
            $this->vars[$fn_name] = $fn;
51 1
            $ops[] = '    $_carry = $' . $fn_name . '($_, $_carry);';
52 1
        } else {
53 8
            $ops[] = '    $_carry = ' . $fn . ';';
54
        }
55 8
        $after = '$_result = $_carry;';
56 8
        return self::evaluate($this->seed, $this->vars, $this->compile($ops), '', $after);
0 ignored issues
show
Bug introduced by
The property seed does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like compile() 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...
57
    }
58
}
59