Completed
Push — master ( 071f6e...2ed909 )
by Seth
25:30
created

SearchResult::sort()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace smtech\StMarksSearch;
4
5
/**
6
 * An object representing a single search result
7
 *
8
 * @author Seth Battis <[email protected]>
9
 */
10
class SearchResult extends ParameterArrayConstructor
11
{
12
    /**
13
     * URL of the search result
14
     * @var string
15
     */
16
    protected $url;
17
18
    /**
19
     * Description of result's relevance
20
     * @var Relevance
21
     */
22
    protected $relevance;
23
24
    /**
25
     * Human-readable title
26
     * @var string
27
     */
28
    protected $title;
29
30
    /**
31
     * Human-readable description
32
     *
33
     * Ideally 20-100 words, may be HTML-formatted (although links should be
34
     * stripped out).
35
     * @var string
36
     */
37
    protected $description;
38
39
    /**
40
     * Simplified description of search domain source of the result
41
     * @var SearchSource
42
     */
43
    protected $source;
44
45
    /**
46
     * Construct a SearchResult
47
     *
48
     * Expects an associative parameter array:
49
     *
50
     * ```
51
     * [
52
     *   'url' => URL of the search result as a string,
53
     *   'title' => Title of the search result as a string,
54
     *   'relevance' => instance of `Relevance`,
55
     *   'source' => instance of `SearchSource`,
56
     *   'description' => Optional: search result descriptin as a string
57
     * ]
58
     * ```
59
     *
60
     * @param mixed[string] $params
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
61
     */
62
    public function __construct($params)
63
    {
64
        $this->requireParameter($params, 'url');
65
        $this->requireParameter($params, 'title');
66
        $this->requireParameter($params, 'relevance', Relevance::class);
67
        $this->requireParameter($params, 'source', SearchSource::class);
68
69
        $this->defaultParameter($params, 'description', '[no description available]');
70
71
        $this->setUrl($params['url']);
1 ignored issue
show
Documentation Bug introduced by
The method setUrl does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
72
        $this->setTitle($params['title']);
1 ignored issue
show
Documentation Bug introduced by
The method setTitle does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
        $this->setRelevance($params['relevance']);
1 ignored issue
show
Documentation Bug introduced by
The method setRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
        $this->setSource($params['source']);
1 ignored issue
show
Documentation Bug introduced by
The method setSource does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
        $this->setDescription($params['description']);
1 ignored issue
show
Documentation Bug introduced by
The method setDescription does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
    }
77
78
    /**
79
     * Sort into order of descending relevance
80
     *
81
     * @param SearchResult[] $results
82
     * @return void
83
     */
84
    public static function sort(&$results)
85
    {
86
        usort($results, function (SearchResult $a, SearchResult $b) {
87
            if ($a->getRelevance()->getScore() < $b->getRelevance()->getScore()) {
1 ignored issue
show
Documentation Bug introduced by
The method getRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
                return 1;
89
            } elseif ($a->getRelevance()->getScore() > $b->getRelevance()->getScore()) {
1 ignored issue
show
Documentation Bug introduced by
The method getRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
                return -1;
91
            } else {
92
                return 0;
93
            }
94
        });
95
    }
96
}
97