CheckAnwerV0   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 69
Duplicated Lines 11.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C checkAnswers() 8 59 18

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 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