Helper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 40
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 4
A camelCase() 0 10 1
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition;
4
5
/**
6
 * Helper class
7
 *
8
 * This class defines static utility functions
9
 */
10
class Helper
11
{
12
    /**
13
     * Initialize an object with a given array of parameters
14
     *
15
     * Parameters are automatically converted to camelCase. Any parameters which do
16
     * not match a setter on the target object are ignored.
17
     *
18
     * @param Object $target     The object to set parameters on
19
     * @param array $parameters An array of parameters to set
20
     */
21 42
    public static function initialize($target, $parameters)
22
    {
23 42
        if (is_array($parameters)) {
24 42
            foreach ($parameters as $key => $value) {
25 36
                $method = 'set'.ucfirst(static::camelCase($key));
26 36
                if (method_exists($target, $method)) {
27 36
                    $target->$method($value);
28 36
                }
29 42
            }
30 42
        }
31 42
    }
32
33
    /**
34
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
35
     *
36
     * @param  string  $str The input string
37
     * @return string camelCased output string
38
     */
39 36
    public static function camelCase($str)
40
    {
41 36
        return preg_replace_callback(
42 36
            '/_([a-z])/',
43 36
            function ($match) {
44 20
                return strtoupper($match[1]);
45 36
            },
46
            $str
47 36
        );
48
    }
49
}
50