GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( aacd2f...926faa )
by Florian
04:47
created

DummySearchService::search()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 32
rs 6.7272
cc 7
eloc 21
nc 10
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Stinger Enity Search package.
5
 *
6
 * (c) Oliver Kotte <[email protected]>
7
 * (c) Florian Meyer <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace StingerSoft\EntitySearchBundle\Services;
13
14
use StingerSoft\EntitySearchBundle\Model\Document;
15
use StingerSoft\EntitySearchBundle\Model\Query;
16
use StingerSoft\EntitySearchBundle\Model\ResultSetAdapter;
17
use StingerSoft\EntitySearchBundle\Model\Result\FacetSetAdapter;
18
use StingerSoft\EntitySearchBundle\Model\Result\FacetSet;
19
use StingerSoft\EntitySearchBundle\Model\Result\FacetAdapter;
20
21
class DummySearchService extends AbstractSearchService {
22
23
	/**
24
	 *
25
	 * @var Document[]
26
	 */
27
	protected $index = array();
28
29
	protected function createDocumentId(Document $document) {
30
		$id = $document->getEntityClass();
31
		$id .= '#';
32
		$entityId = $document->getEntityId();
33
		if(is_scalar($entityId)) {
34
			$id .= $entityId;
35
		} else {
36
			$id .= md5(serialize($entityId));
37
		}
38
		return $id;
39
	}
40
41
	/**
42
	 *
43
	 * {@inheritDoc}
44
	 *
45
	 * @see \StingerSoft\EntitySearchBundle\Services\SearchService::clearIndex()
46
	 */
47
	public function clearIndex() {
48
		$this->index = array();
49
	}
50
51
	/**
52
	 *
53
	 * {@inheritDoc}
54
	 *
55
	 * @see \StingerSoft\EntitySearchBundle\Services\SearchService::saveDocument()
56
	 */
57
	public function saveDocument(Document $document) {
58
		$this->index[$this->createDocumentId($document)] = $document;
59
	}
60
61
	/**
62
	 *
63
	 * {@inheritDoc}
64
	 *
65
	 * @see \StingerSoft\EntitySearchBundle\Services\SearchService::removeDocument()
66
	 */
67
	public function removeDocument(Document $document) {
68
		unset($this->index[$this->createDocumentId($document)]);
69
	}
70
71
	/**
72
	 *
73
	 * {@inheritDoc}
74
	 *
75
	 * @see \StingerSoft\EntitySearchBundle\Services\SearchService::autocomplete()
76
	 */
77
	public function autocomplete($search, $maxResults = 10) {
78
		$words = array();
79
		foreach($this->index as $doc) {
80
			foreach($doc->getFields() as $content) {
81
				if(is_string($content)) {
82
					$words = array_merge($words, explode(' ', $content));
83
				}
84
			}
85
		}
86
		
87
		return array_filter($words, function ($word) use ($search) {
88
			return stripos($word, $search) === 0;
89
		});
90
	}
91
92
	/**
93
	 *
94
	 * {@inheritDoc}
95
	 *
96
	 * @see \StingerSoft\EntitySearchBundle\Services\SearchService::search()
97
	 */
98
	public function search(Query $query) {
99
		$term = $query->getSearchTerm();
100
101
		$result = new ResultSetAdapter();
102
		$facets = new FacetSetAdapter();
103
		
104
		
105
		$hits = array();
106
		foreach($this->index as $key => $doc) {
107
			foreach($doc->getFields() as $content) {
108
				if(is_string($content) && stripos($content, $term) !== false) {
109
					if(!isset($hits[$key])) {
110
						$hits[$key] = 0;
111
					}
112
					$hits[$key] = $hits[$key] + 1;
113
				}
114
			}
115
		}
116
		arsort($hits);
117
		$results = array();
118
		foreach(array_keys($hits) as $docId){
119
			$doc = $this->index[$docId];
120
			$facets->addFacetValue(FacetSet::FACET_ENTITY_TYPE, $doc->getEntityClass());
121
			$facets->addFacetValue(FacetSet::FACET_AUTHOR, $doc->getFieldValue(Document::FIELD_AUTHOR));
122
			$results[] = $doc;
123
		}
124
		
125
		$result->setResults($results);
126
		$result->setFacets($facets);
127
		
128
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (StingerSoft\EntitySearch...\Model\ResultSetAdapter) is incompatible with the return type declared by the interface StingerSoft\EntitySearch...s\SearchService::search of type StingerSoft\EntitySearchBundle\Services\ResultSet.

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...
129
	}
130
}