TransformTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 35.82 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 24
loc 67
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 12 12 2
A mapWithKey() 12 12 2
A column() 0 16 3
A flip() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Spindle\Collection\Traits;
3
4
trait TransformTrait
5
{
6
    /**
7
     * @param string|callable $fn ($val) => $val
8
     * @return \Spindle\Collection\Collection
9
     */
10 11 View Code Duplication
    public function map($fn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    {
12 11
        $this->is_array = false;
0 ignored issues
show
Bug introduced by
The property is_array 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...
13 11
        if (is_callable($fn)) {
14 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...
15 1
            $this->vars[$fn_name] = $fn;
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...
16 1
            $this->ops[] = '    $_ = $' . $fn_name . '($_);';
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...
17 1
        } else {
18 11
            $this->ops[] = '    $_ = ' . $fn . ';';
19
        }
20 11
        return $this->step();
0 ignored issues
show
Bug introduced by
It seems like step() 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
     * @param string|callable $fn ($key, $val) => $val
25
     * @return \Spindle\Collection\Collection
26
     */
27 1 View Code Duplication
    public function mapWithKey($fn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29 1
        $this->is_array = false;
30 1
        if (is_callable($fn)) {
31 1
            $fn_name = '_fn' . $this->fn_cnt++;
32 1
            $this->vars[$fn_name] = $fn;
33 1
            $this->ops[] = '    $_ = $' . $fn_name . '($_key, $_);';
34 1
        } else {
35 1
            $this->ops[] = '    $_ = ' . $fn . ';';
36
        }
37 1
        return $this->step();
0 ignored issues
show
Bug introduced by
It seems like step() 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...
38
    }
39
40
    /**
41
     * @param string|string[] columns
42
     * @return \Spindle\Collection\Collection
43
     */
44 2
    public function column($columns)
45
    {
46 2
        $this->is_array = false;
47 2
        if (is_array($columns)) {
48 1
            $defs = [];
49 1
            foreach ($columns as $key) {
50 1
                $exported = var_export($key, 1);
51 1
                $defs[] = "$exported => \$_[$exported]";
52 1
            }
53 1
            $this->ops[] = '    $_ = [' . implode(',', $defs) . '];';
54 1
        } else {
55 1
            $exported = var_export($columns, 1);
0 ignored issues
show
Unused Code introduced by
$exported is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56 1
            $this->ops[] = "    \$_ = \$_[$columns];";
57
        }
58 2
        return $this->step();
0 ignored issues
show
Bug introduced by
It seems like step() 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...
59
    }
60
61
    /**
62
     * @return \Spindle\Collection\Collection
63
     */
64 1
    public function flip()
65
    {
66 1
        $this->is_array = false;
67 1
        $this->ops[] = '    list($_key, $_) = [$_, $_key];';
68 1
        return $this->step();
0 ignored issues
show
Bug introduced by
It seems like step() 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...
69
    }
70
}
71