CheckAnwerV0::checkAnswers()   C
last analyzed

Complexity

Conditions 18
Paths 90

Size

Total Lines 59
Code Lines 37

Duplication

Lines 8
Ratio 13.56 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 8
loc 59
ccs 0
cts 49
cp 0
rs 6.3239
cc 18
eloc 37
nc 90
nop 1
crap 342

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 CheckAnwerV0
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 = count($originalAnswers);
31
32
        $sumTrueCorrect = 0;
33
        $sumAllCorrect = 0;
34
        $countAllTrueAnswers = 0;
35
        $countFalseAnswers = 0;
36
        $countAllFalseAnswers = 0;
37
38
        foreach ($originalAnswers as $item) {
39
            $item->getCorrectly() ? $countAllTrueAnswers++ : $countAllFalseAnswers++;
40
            if ($item->getCorrectly() === $data["answer_{$item->getId()}"]) {
41
                if ($item->getCorrectly() === true) {
42
                    $sumTrueCorrect++;
43
                }
44
                $sumAllCorrect++;
45
            } else {
46
                if ($item->getCorrectly() === false) {
47
                    $countFalseAnswers++;
48
                }
49
            }
50
        }
51
        //first type question
52 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...
53
            if ($question->getAllIncorrect() === $data['answer_all_incorrect'] && $question->getAllIncorrect() === true && $sumTrueCorrect == 0) {
54
55
                return 1;
56
            }
57
58
            return 0;
59
        }
60
61
        // second type question
62
        if ($countAllTrueAnswers == 1) {
63
            if ($countFalseAnswers == 1) {
64
                $result = $sumTrueCorrect != 0 ? 1 / $sumTrueCorrect / 2 : 0;
65
            } elseif ($countFalseAnswers > 1) {
66
                $result = 0;
67
            }
68
            else {
69
                $result = $sumTrueCorrect != 0 ? 1 / $sumTrueCorrect : 0;
70
            }
71
            //third type question
72
        } else {
73
            if ($countAllTrueAnswers > 1) {
74
                $result = $sumAllCorrect != 0 ? ((1 / $countOriginalAnswers) * $sumAllCorrect) : 0;
75
            } else {
76
                $result =  0;
77
            }
78
        }
79
80
        return $result;
81
    }
82
}
83