Completed
Push — master ( dce9c2...90c6b3 )
by Branko
10s
created

EnumerateImagesMissingFacesTask::do()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
25
26
use OCP\IDBConnection;
27
28
use OCA\FaceRecognition\Db\Image;
29
use OCA\FaceRecognition\Db\ImageMapper;
30
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
31
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
32
33
/**
34
 * Task that gets all images (from database) that don't yet have faces found (e.g. they are not processed).
35
 * Shuffles found images and outputs them to context->propertyBag.
36
 */
37
class EnumerateImagesMissingFacesTask extends FaceRecognitionBackgroundTask {
38
	/** @var IDBConnection DB connection*/
39
	protected $connection;
40
41
	/** @var ImageMapper Image mapper*/
42
	protected $imageMapper;
43
44
	/**
45
	 * @param IDBConnection $connection DB connection
46
	 * @param ImageMapper $imageMapper Image mapper
47
	 */
48
	public function __construct(IDBConnection $connection, ImageMapper $imageMapper) {
49
		parent::__construct();
50
		$this->connection = $connection;
51
		$this->imageMapper = $imageMapper;
52
	}
53
54
	/**
55
	 * @inheritdoc
56
	 */
57
	public function description() {
58
		return "Find all images which don't have faces generated for them";
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64
	public function execute(FaceRecognitionContext $context) {
65
		$this->setContext($context);
66
67
		$images = $this->imageMapper->findImagesWithoutFaces($this->context->user);
68
		yield;
69
70
		shuffle($images);
71
		$this->context->propertyBag['images'] = $images;
72
73
		return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by OCA\FaceRecognition\Back...ssingFacesTask::execute of type Generator.

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...
74
	}
75
}