MergedDescription   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 104
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B fromCdbDetail() 0 28 7
A merge() 0 30 3
A getShortDescriptionUDB2FormattingFilter() 0 7 2
A getShortDescriptionUDB3FormattingFilter() 0 7 2
1
<?php
2
3
namespace CultuurNet\UDB3\Cdb\Description;
4
5
use CultuurNet\UDB3\StringFilter\StringFilterInterface;
6
use ValueObjects\StringLiteral\StringLiteral;
7
8
class MergedDescription extends StringLiteral
9
{
10
    /**
11
     * @var StringFilterInterface
12
     */
13
    private static $shortDescriptionUDB2FormattingFilter;
14
15
    /**
16
     * @var StringFilterInterface
17
     */
18
    private static $shortDescriptionUDB3FormattingFilter;
19
20
    /**
21
     * @param \CultureFeed_Cdb_Data_Detail $detail
22
     * @return MergedDescription
23
     * @throws \InvalidArgumentException
24
     */
25
    public static function fromCdbDetail(\CultureFeed_Cdb_Data_Detail $detail)
26
    {
27
        $longDescription = $detail->getLongDescription();
28
        if ($longDescription) {
29
            $longDescription = LongDescription::fromCdbXmlToJsonLdFormat($longDescription);
30
        }
31
32
        $shortDescription = $detail->getShortDescription();
33
        if ($shortDescription) {
34
            $shortDescription = ShortDescription::fromCdbXmlToJsonLdFormat($shortDescription);
35
        }
36
37
        if ($longDescription && $shortDescription) {
38
            return MergedDescription::merge($shortDescription, $longDescription);
0 ignored issues
show
Bug introduced by
It seems like $shortDescription defined by $detail->getShortDescription() on line 32 can also be of type string; however, CultuurNet\UDB3\Cdb\Desc...gedDescription::merge() does only seem to accept object<CultuurNet\UDB3\C...ption\ShortDescription>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $longDescription defined by $detail->getLongDescription() on line 27 can also be of type string; however, CultuurNet\UDB3\Cdb\Desc...gedDescription::merge() does only seem to accept object<CultuurNet\UDB3\C...iption\LongDescription>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
        }
40
41
        if ($longDescription) {
42
            return new MergedDescription($longDescription->toNative());
43
        }
44
45
        if ($shortDescription) {
46
            return new MergedDescription($shortDescription->toNative());
47
        }
48
49
        throw new \InvalidArgumentException(
50
            'Could not create MergedDescription object from given ' . get_class($detail) . '.'
51
        );
52
    }
53
54
    /**
55
     * @param ShortDescription $shortDescription
56
     * @param LongDescription $longDescription
57
     * @return MergedDescription $longDescription
58
     */
59
    public static function merge(ShortDescription $shortDescription, LongDescription $longDescription)
60
    {
61
        $shortAsString = $shortDescription->toNative();
62
        $longAsString = $longDescription->toNative();
63
64
        $shortAsStringWithoutEllipsis = rtrim($shortAsString, '. ');
65
66
        $longFormattedAsUdb2Short = self::getShortDescriptionUDB2FormattingFilter()->filter($longAsString);
67
        $longFormattedAsUdb3Short = self::getShortDescriptionUDB3FormattingFilter()->filter($longAsString);
68
69
        $udb2Comparison = strncmp(
70
            $longFormattedAsUdb2Short,
71
            $shortAsStringWithoutEllipsis,
72
            mb_strlen($shortAsStringWithoutEllipsis)
73
        );
74
75
        $udb3Comparison = strncmp(
76
            $longFormattedAsUdb3Short,
77
            $shortAsStringWithoutEllipsis,
78
            mb_strlen($shortAsStringWithoutEllipsis)
79
        );
80
81
        $shortIncludedInLong = $udb2Comparison === 0 || $udb3Comparison === 0;
82
83
        if ($shortIncludedInLong) {
84
            return new MergedDescription($longAsString);
85
        } else {
86
            return new MergedDescription($shortAsString . PHP_EOL . PHP_EOL . $longAsString);
87
        }
88
    }
89
90
    /**
91
     * @return StringFilterInterface
92
     */
93
    private static function getShortDescriptionUDB2FormattingFilter()
94
    {
95
        if (!isset(self::$shortDescriptionUDB2FormattingFilter)) {
96
            self::$shortDescriptionUDB2FormattingFilter = new ShortDescriptionUDB2FormattingFilter();
97
        }
98
        return self::$shortDescriptionUDB2FormattingFilter;
99
    }
100
101
    /**
102
     * @return StringFilterInterface
103
     */
104
    private static function getShortDescriptionUDB3FormattingFilter()
105
    {
106
        if (!isset(self::$shortDescriptionUDB3FormattingFilter)) {
107
            self::$shortDescriptionUDB3FormattingFilter = new ShortDescriptionUDB3FormattingFilter();
108
        }
109
        return self::$shortDescriptionUDB3FormattingFilter;
110
    }
111
}
112