Combiners   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 116
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 5 1
A hp$0 ➔ __invoke() 0 17 4
A hp$1 ➔ __construct() 0 5 1
A hp$1 ➔ __invoke() 0 17 4
B appender() 0 27 4
B prefixed() 0 27 4
A hp$2 ➔ __construct() 0 7 1
B hp$2 ➔ __invoke() 0 16 7
C datetime() 0 28 7
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Filter;
22
23
/**
24
 * Come Together Right Now.
25
 *
26
 * This class provides some Reducers.
27
 */
28
class Combiners
29
{
30
    /**
31
     * A combiner that takes any field with a given prefix and adds it to a List
32
     *
33
     * For example, `value1`, `value2`, and `value3` can be combined into an
34
     * outgoing field `value` with an array that contains 3 entries.
35
     *
36
     * @param string $destination The outgoing field name
37
     * @param string $prefix The prefix to find, uses the destination by default
38
     * @return \Caridea\Filter\Reducer The created filter
39
     */
40 2
    public static function appender(string $destination, string $prefix = null): Reducer
41
    {
42
        return new class($destination, $prefix) implements Reducer {
43
            public function __construct($destination, $prefix = null)
44
            {
45 2
                $this->destination = $destination;
0 ignored issues
show
Bug introduced by
The property destination 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...
46 2
                $this->prefix = $prefix ?? $destination;
0 ignored issues
show
Bug introduced by
The property prefix 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 2
            }
48
            public function __invoke(array $input): array
49
            {
50 2
                $subl = strlen($this->prefix);
51 2
                $out = [];
52 2
                $mine = [];
53 2
                foreach ($input as $k => $v) {
54 2
                    if (substr($k, 0, $subl) === $this->prefix) {
55 1
                        $mine[] = $v;
56
                    } else {
57 2
                        $out[$k] = $v;
58
                    }
59
                }
60 2
                if ($mine) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mine of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61 1
                    $out[$this->destination] = $mine;
62
                }
63 2
                return $out;
64
            }
65
        };
66
    }
67
68
    /**
69
     * A combiner that combines prefixed fields into a Map.
70
     *
71
     * For example, `address-street` and `address-city` can be combined into an
72
     * outgoing field `address` with `street` and `city` keys.
73
     *
74
     * @param string $destination The outgoing field name
75
     * @param string $prefix The prefix to find, uses the destination by default
76
     * @return \Caridea\Filter\Reducer The created filter
77
     */
78 2
    public static function prefixed(string $destination, string $prefix = null): Reducer
79
    {
80
        return new class($destination, $prefix) implements Reducer {
81
            public function __construct($destination, $prefix)
82
            {
83 2
                $this->destination = $destination;
0 ignored issues
show
Bug introduced by
The property destination 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...
84 2
                $this->prefix = $prefix ?? $destination;
0 ignored issues
show
Bug introduced by
The property prefix 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...
85 2
            }
86
            public function __invoke(array $input): array
87
            {
88 2
                $subl = strlen($this->prefix);
89 2
                $out = [];
90 2
                $mine = [];
91 2
                foreach ($input as $k => $v) {
92 2
                    if (substr($k, 0, $subl) === $this->prefix) {
93 1
                        $mine[substr($k, $subl)] = $v;
94
                    } else {
95 2
                        $out[$k] = $v;
96
                    }
97
                }
98 2
                if ($mine) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mine of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99 1
                    $out[$this->destination] = $mine;
100
                }
101 2
                return $out;
102
            }
103
        };
104
    }
105
106
    /**
107
     * Creates a combiner that combines datetime values.
108
     *
109
     * @param string $destination The outgoing field name
110
     * @param string $date The field to find date (e.g. `2016-09-15`)
111
     * @param string $time The field to find time (e.g. `T12:04:06`)
112
     * @param string $timezone The field to find timezone name (e.g. `America/New_York`)
113
     * @return \Caridea\Filter\Reducer The created filter
114
     */
115
    public static function datetime(string $destination, string $date, string $time, string $timezone = null): Reducer
116
    {
117
        return new class($destination, $date, $time, $timezone) implements Reducer {
118 3
            public function __construct($destination, $date, $time, $timezone = null)
119
            {
120 3
                $this->destination = $destination;
0 ignored issues
show
Bug introduced by
The property destination 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...
121 3
                $this->dfield = $date;
0 ignored issues
show
Bug introduced by
The property dfield 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...
122 3
                $this->tfield = $time;
0 ignored issues
show
Bug introduced by
The property tfield 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...
123 3
                $this->zfield = $timezone;
0 ignored issues
show
Bug introduced by
The property zfield 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...
124 3
            }
125 3
            public function __invoke(array $input): array
126
            {
127 3
                $date = $input[$this->dfield] ?? '';
128 3
                $time = $input[$this->tfield] ?? '';
129 3
                $zone = $this->zfield === null ? null : ($input[$this->zfield] ?? null);
130 3
                $out = array_filter($input, function ($k) {
131 3
                    return $k !== $this->dfield && $k !== $this->tfield && $k !== $this->zfield;
132 3
                }, ARRAY_FILTER_USE_KEY);
133 3
                if ($date || $time) {
134 2
                    $out[$this->destination] = new \DateTime(
135 2
                        "{$date}{$time}",
136 2
                        !$this->zfield ? null : new \DateTimeZone($zone)
137
                    );
138
                }
139 3
                return $out;
140
            }
141
        };
142
    }
143
}
144