Completed
Push — master ( 87b10e...1961d1 )
by Andre
16s queued 12s
created

src/Mapper/AbstractMapper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Nickname;
7
8
abstract class AbstractMapper
9
{
10
    /**
11
     * implements the mapping of parts
12
     *
13
     * @param array $parts - the name parts
14
     * @return array $parts - the mapped parts
15
     */
16
    abstract public function map(array $parts);
17
18
    /**
19
     * checks if there are still unmapped parts left before the given position
20
     *
21
     * @param array $parts
22
     * @param $index
23
     * @return bool
24
     */
25
    protected function hasUnmappedPartsBefore(array $parts, $index): bool
26
    {
27
        foreach ($parts as $k => $part) {
28
            if ($k === $index) {
29
                break;
30
            }
31
32
            if (!($part instanceof AbstractPart)) {
33
                return true;
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * @param string $type
42
     * @param array $parts
43
     * @return int|bool
44
     */
45 View Code Duplication
    protected function findFirstMapped(string $type, array $parts)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $total = count($parts);
48
49
        for ($i = 0; $i < $total; $i++) {
50
            if ($parts[$i] instanceof $type) {
51
                return $i;
52
            }
53
        }
54
55
        return false;
56
    }
57
58
    /**
59
     * get the registry lookup key for the given word
60
     *
61
     * @param string $word the word
62
     * @return string the key
63
     */
64
    protected function getKey($word): string
65
    {
66
        return strtolower(str_replace('.', '', $word));
67
    }
68
}
69