Completed
Push — master ( 10d5be...081b63 )
by Richard
05:06
created

Collections::each()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once dirname(__DIR__) . '/vendor/autoload.php';
3
4
/**
5
 * Class Item
6
 *
7
 * A simple item.
8
 */
9
class Item
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    public $position;
12
}
13
14
/**
15
 * Class ItemCollection
16
 *
17
 * A simple collection of items.
18
 */
19
class ItemCollection extends \RQuadling\TypedArray\TypedArray
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
{
21
    const ARRAY_TYPE = 'Item';
22
}
23
24
/**
25
 * Class Collections
26
 *
27
 * An example abstract collections class that implements an each() method.
28
 */
29
abstract class Collections extends \RQuadling\TypedArray\TypedArray
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     * @param callable $callable
33
     * @return $this
34
     */
35
    public function each(callable $callable)
36
    {
37
        foreach ($this as $key => $value) {
38
            $callable($value, $key);
39
        }
40
41
        return $this;
42
    }
43
}
44
45
/**
46
 * Class ItemCollectionEx
47
 *
48
 * A extended collection of items.
49
 */
50
class ItemCollectionEx extends Collections
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
51
{
52
    const ARRAY_TYPE = 'Item';
53
}
54
55
/**
56
 * Create a new item collection and add a second item to it.
57
 */
58
$items = new ItemCollection([new Item]);
59
$items[] = new Item;
60
61
/**
62
 * Create an extended collection and add a second item to it.
63
 */
64
$itemsEx = new ItemCollectionEx([new Item]);
65
$itemsEx[] = new Item;
66
67
/**
68
 * Use the each() method to set the position property in the item.
69
 */
70
$itemsEx->each(function(Item $item, $key){
71
    $item->position = $key;
72
});
73
74
/**
75
 * Clone the extended items collection.
76
 */
77
$itemsEx2 = clone $itemsEx;
78
79
/**
80
 * Use the each() method to make further changes to the position property of
81
 * the cloned collection.
82
 */
83
$itemsEx2->each(function(Item $item, $key){
84
    $item->position += $key;
85
});
86
87
/**
88
 * Make a new simple collection using the same items as the cloned collection.
89
 */
90
$items2 = new ItemCollection( $itemsEx2);
91
92
/**
93
 * Show the results of all of this work.
94
 *
95
 * Pay special attention to the object #'s.
96
 */
97
98
/**
99
 * Firstly the simple collection #2[#3, #4]
100
 */
101
var_dump($items);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($items); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
102
103
/**
104
 * Secondly the extended collection #5[#6, #7]
105
 */
106
var_dump($itemsEx);
107
108
/**
109
 * Thirdly, the cloned extended collection #8[#11, #12]
110
 */
111
var_dump($itemsEx2);
112
113
/**
114
 * Finally, the copied simple collection #9[#11, #12]
115
 *
116
 * NOTE: These items are the same ones as in $itemsEx2.
117
 */
118
var_dump($items2);
119