CopyBasedIntroduction::truncateWords()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4286
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Axstrad library.
5
 *
6
 * (c) Dan Kempster <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @copyright 2014-2015 Dan Kempster <[email protected]>
12
 */
13
14
namespace Axstrad\Component\Content\Traits;
15
16
/**
17
 * Axstrad\Bundle\ContentBundle\Traits\CopyBasedIntroduction
18
 *
19
 * Property requirements
20
 *   - $copy = null
21
 *   - $introduction = null
22
 *   - $copyIntroWordCount = <integer>
23
 *
24
 * @author Dan Kempster <[email protected]>
25
 * @license MIT
26
 * @package Axstrad/Content
27
 * @since 0.4
28
 */
29
trait CopyBasedIntroduction
30
{
31
    use Copy;
32
    use Introduction {
33
        Introduction::getIntroduction as private internalGetIntroduction;
34
    }
35
36
37
    /**
38
     * Get introduction.
39
     *
40
     * If an introduction hasn't been set, the first {@see $copyIntroWordCount
41
     * X words} from the {@see getCopy copy} are returned.
42
     *
43
     * @param string $ellipse
44
     * @return string
45
     */
46 19
    public function getIntroduction($ellipse = "...")
47
    {
48 19
        $intro = $this->internalGetIntroduction();
0 ignored issues
show
Bug introduced by
The method internalGetIntroduction() does not exist on Axstrad\Component\Conten...s\CopyBasedIntroduction. Did you maybe mean getIntroduction()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
50 19
        if ($intro === null && ($copy = $this->getCopy()) !== null) {
51 3
            $copy = trim(strip_tags($this->getCopy()));
52
53 3
            if ( ! empty($copy)) {
54 3
                $intro = $this->truncateWords($copy, $ellipse);
55 3
            }
56 3
        }
57
58 19
        return $intro;
59
    }
60
61
62
    /**
63
     * @param string $copy
64
     * @param string $ellipse
65
     * @return mixed
66
     */
67 3
    protected function truncateWords($copy, $ellipse)
68
    {
69
        /** @noinspection PhpUndefinedFieldInspection */
70 3
        $words = explode(" ", trim($copy), $this->copyIntroWordCount + 1);
0 ignored issues
show
Bug introduced by
The property copyIntroWordCount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
        /** @noinspection PhpUndefinedFieldInspection */
72 3
        if (count($words) > $this->copyIntroWordCount) {
73 2
            array_pop($words);
74 2
            $intro = trim(implode(" ", $words)).$ellipse;
75 2
        }
76
        else {
77 1
            $intro = $copy;
78
        }
79
80 3
        return $intro;
81
    }
82
}
83