Passed
Push — master ( 8f9ec7...80523f )
by Timo
23:39
created

IsBoostedFieldViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Backend\Document;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 Rafael Kähm <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
28
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
29
30
/**
31
 * Condition for checking if type is a string.
32
 */
33
class IsBoostedFieldViewHelper extends AbstractConditionViewHelper
34
{
35
36
    /**
37
     * Initialize ViewHelper arguments
38
     *
39
     * @return void
40
     */
41
    public function initializeArguments()
42
    {
43
        parent::initializeArguments();
44
        $this->registerArgument('fieldName', 'string', 'Fieldname to be verified.', true);
45
        $this->registerArgument('document', '\Apache_Solr_Document', '\Apache_Solr_Document to be verified.', true);
46
    }
47
48
    /**
49
     * This method decides if the condition is true or false
50
     *
51
     * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper.
52
     * @return bool
53
     */
54 2
    protected static function evaluateCondition($arguments = null)
55
    {
56
        /* @var $document \Apache_Solr_Document */
57 2
        $document = $arguments['document'];
58 2
        return $document->getFieldBoost($arguments['fieldName']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $document->getFie...rguments['fieldName']); (double) is incompatible with the return type of the parent method TYPO3Fluid\Fluid\Core\Vi...lper::evaluateCondition of type boolean.

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...
59
    }
60
}
61