CheckAnwers   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 70
Duplicated Lines 11.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 20
c 6
b 0
f 1
lcom 1
cbo 2
dl 8
loc 70
ccs 0
cts 53
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D checkAnswers() 8 60 19

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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