CheckAnwers::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 04.03.16
6
 * Time: 15:50
7
 */
8
9
namespace AppBundle\Services;
10
11
12
use Symfony\Bridge\Doctrine\RegistryInterface;
13
14
class CheckAnwers
15
{
16
    private $doctrine;
17
18
    public function __construct(RegistryInterface $registryInterface)
19
    {
20
        $this->doctrine = $registryInterface;
21
    }
22
23
    public function checkAnswers(array $data)
24
    {
25
        $em = $this->doctrine->getManager();
26
        $question = $em->getRepository('AppBundle:Question')
27
            ->find($data['idQuestion']);
28
29
        $originalAnswers = $question->getAnswers();
30
        $countOriginalAnswers = $question->getCountAnswers();
31
32
        $sumAllCorrect = 0;
33
        $sumCorrectChecks = 0;
34
        $sumFalseAnswers = 0;
35
        $countAllTrueAnswers = 0;
36
        $countAllChecks = 0;
37
38
        foreach ($originalAnswers as $item) {
39
            $item->getCorrectly() ? $countAllTrueAnswers++ : null;
40
            $data["answer_{$item->getId()}"] ? $countAllChecks++ : null;
41
            if ($item->getCorrectly() === $data["answer_{$item->getId()}"]) {
42
                $sumAllCorrect++;
43
                if ($item->getCorrectly())
44
                    $sumCorrectChecks++;
45
            } else {
46
                if ($item->getCorrectly() === false) {
47
                    $sumFalseAnswers++;
48
                }
49
            }
50
        }
51
52
        if ($countAllTrueAnswers == 1) {
53
            $maxCountAnswers = 2;
54
        } else {
55
            $maxCountAnswers = $countAllTrueAnswers;
56
        }
57
58 View Code Duplication
        if ($question->getAllIncorrect() || $data['answer_all_incorrect']) {
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...
59
            if ($question->getAllIncorrect() === $data['answer_all_incorrect'] && $question->getAllIncorrect() === true &&
60
                $sumAllCorrect == $countOriginalAnswers
61
            )
62
                return 1;
63
64
            return 0;
65
        }
66
67
68
        if ($sumAllCorrect && $sumCorrectChecks && $countAllChecks <= $maxCountAnswers) {
69
            // second type question
70
            if ($countAllTrueAnswers == 1) {
71
                $result = (1 / $maxCountAnswers) * ($sumCorrectChecks + ($sumFalseAnswers > 0 ? 0: 1));
72
            }
73
            // third type question
74
            elseif ($countAllTrueAnswers > 1) {
75
                $result = (1 / $maxCountAnswers) * $sumCorrectChecks;
76
            }
77
        } else {
78
            $result = 0;
79
        }
80
81
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
82
    }
83
}
84