Completed
Push — master ( 0ffa20...1d790a )
by Seth
02:58
created

AnnouncementsSearch   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 37.1 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 23
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 11 33 5
A relevance() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace smtech\StMarksSearch\Canvas\Courses\Announcements;
4
5
use smtech\CanvasPest\CanvasArray;
6
use smtech\CanvasPest\CanvasObject;
7
use smtech\StMarksSearch\Relevance;
8
use smtech\StMarksSearch\SearchResult;
9
use smtech\StMarksSearch\SearchSource;
10
use smtech\StMarksSearch\Canvas\Courses\AbstractCourseSearchDomain;
11
12
/**
13
 * Search a Canvas course's announcements
14
 *
15
 * @author Seth Battis <[email protected]>
16
 */
17
class AnnouncementsSearch extends AbstractCourseSearchDomain
18
{
19
    /**
20
     * Search for `$query` among the announcements
21
     *
22
     * @param string $query
23
     * @return SearchResult[] Ordered by descending relevance
24
     */
25
    public function search($query)
26
    {
27
        /* Canvas doesn't accept search queries shorter than 3 characters */
28
        if (strlen(trim($query)) < 3) {
29
            return [];
30
        }
31
32
        $source = new SearchSource($this);
33
        $results = [];
34
35
        $response = $this->getApi()->get(
36
            'courses/' . $this->getId() . '/discussion_topics',
37
            [
0 ignored issues
show
Documentation introduced by
array('search_term' => $...announcements' => true) is of type array<string,string|bool...ouncements":"boolean"}>, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
                'search_term' => $query,
39
                'only_announcements' => true
40
            ]
41
        );
42
43 View Code Duplication
        if (is_a($response, CanvasArray::class)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
            foreach ($response as $announcement) {
0 ignored issues
show
Bug introduced by
The expression $response of type object<smtech\CanvasPest...CanvasPest\CanvasArray> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
                $results[] = new SearchResult(
46
                    $announcement['html_url'],
47
                    $this->relevance($announcement, $query),
48
                    $announcement['title'],
49
                    (empty($announcement['message']) ? '' : substr(str_replace(PHP_EOL, ' ', strip_tags($announcement['message'])), 0, 255) . '&hellip;'),
50
                    $source
51
                );
52
            }
53
        }
54
55
        $this->sortByRelevance($results);
56
        return $results;
57
    }
58
59
    /**
60
     * Calculate the relevance of a particular announcement
61
     *
62
     * @param CanvasObject $announcement
63
     * @param string $query
64
     * @return Relevance
65
     */
66 View Code Duplication
    protected function relevance($announcement, $query)
0 ignored issues
show
Duplication introduced by
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...
67
    {
68
        $relevance = new Relevance();
69
70
        $relevance->add(Relevance::stringProportion($announcement['title'], $query), 'title match');
71
72
        if (($count = substr_count(strtolower($announcement['message']), strtolower($query))) > 0) {
73
            $relevance->add($count * 0.01, "$count occurrences in body");
74
        }
75
76
        return $relevance;
77
    }
78
}
79