Completed
Push — v2 ( 88b419...d5d6a3 )
by Joschi
05:06
created

ProfiledNameFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.43%

Importance

Changes 0
Metric Value
dl 0
loc 129
ccs 37
cts 46
cp 0.8043
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromArguments() 0 12 2
B consumeProfiledName() 0 23 5
A createProfiledNameFromObject() 0 16 4
A createProfiledNameFromArray() 0 19 4
A createProfiledNameFromString() 0 15 3
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Infrastructure
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Micrometa\Infrastructure\Factory;
38
39
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException;
40
41
/**
42
 * Profiled name factory
43
 *
44
 * @package Jkphl\Micrometa
45
 * @subpackage Jkphl\Micrometa\Infrastructure
46
 */
47
class ProfiledNameFactory
48
{
49
    /**
50
     * Create a list of profiled names from function arguments
51
     *
52
     * @param array $args Arguments
53
     * @return array Profiled names
54
     */
55 11
    public static function createFromArguments($args)
56
    {
57 11
        $profiledNames = [];
58 11
        $profile = false;
59
60
        // Consume and register all given names and profiles
61 11
        while (count($args)) {
62 11
            $profiledNames[] = self::consumeProfiledName($args, $profile);
0 ignored issues
show
Security Bug introduced by
It seems like $profile defined by false on line 58 can also be of type false; however, Jkphl\Micrometa\Infrastr...::consumeProfiledName() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
63 11
        }
64
65 11
        return $profiledNames;
66
    }
67
68
    /**
69
     * Create a single profiled name by argument consumption
70
     *
71
     * @param array $args Arguments
72
     * @param string $profile Profile
73
     * @return \stdClass Profiled name
74
     */
75 11
    protected static function consumeProfiledName(&$args, &$profile)
76
    {
77 11
        $profiledName = new \stdClass();
78 11
        $profiledName->profile = $profile;
79
80
        // Get the first argument
81 11
        $arg = array_shift($args);
82
83
        // If it's an object argument
84 11
        if (is_object($arg)) {
85 6
            return self::createProfiledNameFromObject($arg, $profile);
86
        }
87
88
        // If it's an array argument
89 9
        if (is_array($arg)) {
90 2
            return self::createProfiledNameFromArray($arg, $profile);
91
        }
92
93 7
        if (($profile === false) && is_string(current($args))) {
94 3
            $profile = array_shift($args);
95 3
        }
96 7
        return self::createProfiledNameFromString(strval($arg), $profile);
97
    }
98
99
    /**
100
     * Create a profiled name from an object argument
101
     *
102
     * @param \stdClass $arg Object argument
103
     * @param string $profile Profile
104
     * @return \stdClass Profiled name
105
     * @throws InvalidArgumentException If the name is missing
106
     */
107 7
    protected static function createProfiledNameFromObject($arg, &$profile)
108
    {
109
        // If the name is invalid
110 7
        if (!isset($arg->name)) {
111
            throw new InvalidArgumentException(
112
                InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR,
113
                InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME
114
            );
115
        }
116
117 7
        if (isset($arg->profile)) {
118 7
            $profile = trim($arg->profile) ?: null;
119 7
        }
120
121 7
        return self::createProfiledNameFromString($arg->name, $profile);
122
    }
123
124
    /**
125
     * Create a profiled name from an array argument
126
     *
127
     * @param array $arg Array argument
128
     * @param string $profile Profile
129
     * @return \stdClass Profiled name
130
     * @throws InvalidArgumentException If the array definition is invalid
131
     */
132 2
    protected static function createProfiledNameFromArray(array $arg, &$profile)
133
    {
134
        // If it's an associative array containing a "name" key
135 2
        if (array_key_exists('name', $arg)) {
136 1
            return self::createProfiledNameFromObject((object)$arg, $profile);
137
        }
138
139
        // If the argument has two items at least
140 1
        if (count($arg) > 1) {
141 1
            $name = array_shift($arg);
142 1
            $profile = trim(array_shift($arg)) ?: null;
143 1
            return self::createProfiledNameFromString($name, $profile);
144
        }
145
146
        throw new InvalidArgumentException(
147
            InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY_STR,
148
            InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY
149
        );
150
    }
151
152
    /**
153
     * Create a profiled name from string arguments
154
     *
155
     * @param string $name Name
156
     * @param string|null $profile Profile
157
     * @return \stdClass Profiled name
158
     * @throws InvalidArgumentException If the name is invalid
159
     */
160 11
    protected static function createProfiledNameFromString($name, $profile)
161
    {
162
        // If the name is invalid
163 11
        if (!strlen(trim($name))) {
164
            throw new InvalidArgumentException(
165
                InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR,
166
                InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME
167
            );
168
        }
169
170
        return (object)[
171 11
            'name' => trim($name),
172 11
            'profile' => trim($profile) ?: null,
173 11
        ];
174
    }
175
}
176