Completed
Push — signal_search_issues ( 5556b2...f328ba )
by André
63:06 queued 07:22
created

IdentifierMapper::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the IdentifierMapper document field value mapper class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper;
12
13
use eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper;
14
use eZ\Publish\SPI\Search\FieldType\IdentifierField;
15
use eZ\Publish\SPI\Search\Field;
16
17
/**
18
 * Maps IdentifierField document field values to something Elasticsearch can index.
19
 */
20 View Code Duplication
class IdentifierMapper extends FieldValueMapper
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * Check if field can be mapped.
24
     *
25
     * @param \eZ\Publish\SPI\Search\Field $field
26
     *
27
     * @return bool
28
     */
29
    public function canMap(Field $field)
30
    {
31
        return $field->type instanceof IdentifierField;
0 ignored issues
show
Documentation introduced by
The property $type is declared protected in eZ\Publish\SPI\Search\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
    }
33
34
    /**
35
     * Map field value to a proper Elasticsearch representation.
36
     *
37
     * @param Field $field
38
     *
39
     * @return mixed
40
     */
41
    public function map(Field $field)
42
    {
43
        return $this->convert($field->value);
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\SPI\Search\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
    }
45
46
    /**
47
     * Convert to a proper Elasticsearch representation.
48
     *
49
     * @param mixed $value
50
     *
51
     * @return string
52
     */
53
    protected function convert($value)
54
    {
55
        // Remove non-printable characters
56
        return preg_replace('([^A-Za-z0-9/]+)', '', $value);
57
    }
58
}
59