ArticleData   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 13
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 126
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A push() 0 4 1
A findNearest() 0 8 2
C recommend() 0 32 8
A getWordCount() 0 10 1
1
<?php
2
3
namespace stojg\recommend;
4
5
use stojg\recommend\strategy\Cosine;
6
7
/**
8
 * Description of ArticleData.
9
 */
10
class ArticleData extends Data
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $stopWords = ['a', 'about', 'above', 'above', 'across', 'after', 'afterwards', 'again', 'against',
16
        'all', 'almost', 'alone', 'along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst',
17
        'amoungst', 'amount', 'an', 'and', 'another', 'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere',
18
        'are', 'around', 'as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been',
19
        'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both',
20
        'bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de',
21
        'describe', 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight', 'either', 'eleven', 'else',
22
        'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone', 'everything', 'everywhere',
23
        'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly',
24
        'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has', 'hasnt',
25
        'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers', 'herself', 'him',
26
        'himself', 'his', 'how', 'however', 'hundred', "i'd", 'ie', 'if', 'in', 'inc', 'indeed', 'interest', 'into',
27
        'is', 'it', 'its', 'itself', 'keep', 'last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many',
28
        'may', 'me', 'meanwhile', 'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must',
29
        'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine', 'no', 'nobody', 'none',
30
        'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once', 'one', 'only', 'onto',
31
        'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'part', 'per',
32
        'perhaps', 'please', 'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious',
33
        'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow',
34
        'someone', 'something', 'sometime', 'sometimes', 'somewhere', 'still', 'such', 'system', 'take', 'ten', 'than',
35
        'that', 'the', 'their', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore',
36
        'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this', 'those', 'though', 'three',
37
        'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward', 'towards', 'twelve',
38
        'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we', 'well', 'were', 'what',
39
        'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon',
40
        'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'will',
41
        'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves', 'the', ];
42
43
    /**
44
     * @param array $set
0 ignored issues
show
Bug introduced by
There is no parameter named $set. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     */
46
    public function __construct()
47
    {
48
        // Noop - We want to start off with a clean set and use the push method to add
49
        // data to the set
50
    }
51
52
    /**
53
     * @param string $identifier
54
     * @param string $content
55
     */
56
    public function push($identifier, $content)
57
    {
58
        $this->set[$identifier] = $this->getWordCount($content);
59
    }
60
61
    /**
62
     * @param string $for
63
     * @param object $strategy
64
     */
65
    public function findNearest($for, $strategy = null)
66
    {
67
        if ($strategy === null) {
68
            $strategy = new Cosine();
69
        }
70
71
        return parent::findNearest($for, $strategy);
72
    }
73
74
    /**
75
     * Return a list of recommendations.
76
     *
77
     * @param string $for      - the item we want recommendations for
78
     * @param object $strategy
79
     *
80
     * @return array - return a list of identifier ordered by closest
81
     */
82
    public function recommend($for, $strategy = null)
83
    {
84
        if ($strategy === null) {
85
            $strategy = new Cosine();
86
        }
87
88
        $distances = [];
89
        foreach ($this->set as $key => $itemData) {
90
            if ($key == $for) {
91
                continue;
92
            }
93
            $distance = $strategy->run($itemData, $this->set[$for]);
94
            if ($distance === false) {
95
                continue;
96
            }
97
            $distances[$key] = ['key' => $key, 'value' => $distance];
98
        }
99
        if (!count($distances)) {
100
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method stojg\recommend\Data::recommend of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
101
        }
102
        $this->sort($distances, true);
103
104
        $data = [];
105
        foreach ($distances as $article) {
106
            if ($article['value'] == 0) {
107
                continue;
108
            }
109
            $data[] = $article['key'];
110
        }
111
112
        return $data;
113
    }
114
115
    /**
116
     * Get an array of words from the content and a count of how many times
117
     * they appear in the text.
118
     * 
119
     * Note that this method is naive and can't tell the similarity between 'bird' and 'birds'.
120
     * 
121
     * @param string $content
122
     *
123
     * @return array
124
     */
125
    protected function getWordCount($content)
126
    {
127
        $content = strip_tags(strtolower((preg_replace('/\s+/', ' ', $content))));
128
        $words = str_word_count($content, 1);
129
        $filteredWords = array_diff($words, $this->stopWords);
130
        $countedWords = array_count_values($filteredWords);
131
        ksort($countedWords);
132
133
        return $countedWords;
134
    }
135
}
136