Completed
Push — master ( 930ba5...99d635 )
by Narcotic
29:20
created

FieldTitleMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 27
ccs 11
cts 12
cp 0.9167
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B map() 0 18 6
1
<?php
2
/**
3
 * Verify require Title for fields. Use fieldName otherwise
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator\ResourceGenerator;
7
8
/**
9
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
10
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
11
 * @link     http://swisscom.ch
12
 */
13
class FieldTitleMapper implements FieldMapperInterface
14
{
15
    /**
16
     * @param array $field   mappable field with type attribute
17
     * @param mixed $context context for mapper to check
18
     *
19
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
20
     */
21 8
    public function map($field, $context = null)
22
    {
23 8
        $title = 'Please add title';
24
25 8
        if (array_key_exists('title', $field) && !empty($field['title'])) {
26
            $title = $field['title'];
27 8
        } elseif (array_key_exists('fieldName', $field) && !empty($field['fieldName'])) {
28 8
            $value = $field['fieldName'];
29
            // Field have dots
30 8
            if (strpos($value, '.') !== false) {
31 4
                $value = str_replace('.', ' ', str_replace('.0', '.array', $value));
32
            }
33 8
            $value = preg_replace('/(?<=\\w)(?=[A-Z])/', " $1", $value);
34 8
            $title = ucfirst(strtolower($value));
35
        }
36 8
        $field['title'] = trim($title);
37 8
        return $field;
38
    }
39
}
40