Test Setup Failed
Push — development ( 057645...ac5058 )
by Ashutosh
15:58
created

BasePromotionController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 6
loc 69
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 9 2
B findCost() 0 23 6
A findCostAfterDiscount() 6 32 5

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
namespace App\Http\Controllers\Payment;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Plan;
7
use App\Model\Payment\PlanPrice;
8
use App\Model\Payment\Promotion;
9
use App\Model\Product\Product;
10
11
class BasePromotionController extends Controller
12
{
13
    public function getCode()
14
    {
15
        try {
16
            $code = str_random(6);
17
            echo strtoupper($code);
18
        } catch (\Exception $ex) {
19
            return redirect()->back()->with('fails', $ex->getMessage());
20
        }
21
    }
22
23
    public function findCost($type, $value, $price, $productid)
24
    {
25
        try {
26
            switch ($type) {
27
                case 1:
28
                    $percentage = $price * ($value / 100);
29
30
                    return  $price - $percentage;
31
                case 2:
32
                    return $price - $value;
33
                case 3:
34
                    \Cart::update($productid, [
35
                        'price' => $value,
36
                    ]);
37
38
                    return '-0';
39
                case 4:
40
                    return '-'.$price;
41
            }
42
        } catch (\Exception $ex) {
43
            throw new \Exception(\Lang::get('message.find-cost-error'));
44
        }
45
    }
46
47
    public function findCostAfterDiscount($promoid, $productid)
48
    {
49
        try {
50
            $promotion = Promotion::findOrFail($promoid);
51
            $product = Product::findOrFail($productid);
52
            $promotion_type = $promotion->type;
0 ignored issues
show
Coding Style introduced by
$promotion_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
            $promotion_value = $promotion->value;
0 ignored issues
show
Coding Style introduced by
$promotion_value does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
54
            $product_price = 0;
0 ignored issues
show
Coding Style introduced by
$product_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
55
            $userid = \Auth::user()->id;
56
            $control = new \App\Http\Controllers\Order\RenewController();
57
            $cart_control = new \App\Http\Controllers\Front\CartController();
0 ignored issues
show
Coding Style introduced by
$cart_control does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
58
            $currency = $cart_control->checkCurrencySession();
0 ignored issues
show
Coding Style introduced by
$cart_control does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
59
            if ($cart_control->checkPlanSession() === true) {
0 ignored issues
show
Coding Style introduced by
$cart_control does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
60
                $planid = \Session::get('plan');
61
            }
62 View Code Duplication
            if ($product->subscription != 1) {
63
                $planId = Plan::where('product', $productid)->pluck('id')->first();
64
                $product_price = PlanPrice::where('plan_id', $planId)->where('currency', $currency)->pluck('add_price')->first();
0 ignored issues
show
Coding Style introduced by
$product_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
65
            } else {
66
                $product_price = $control->planCost($planid, $userid);
0 ignored issues
show
Coding Style introduced by
$product_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Bug introduced by
The variable $planid 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...
67
            }
68
            if (count(\Cart::getContent())) {
69
                $product_price = \Cart::getSubTotalWithoutConditions();
0 ignored issues
show
Coding Style introduced by
$product_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
70
                // dd($product_price);
71
            }
72
            $updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid);
0 ignored issues
show
Coding Style introduced by
$updated_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
73
74
            return $updated_price;
0 ignored issues
show
Coding Style introduced by
$updated_price does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
75
        } catch (\Exception $ex) {
76
            throw new \Exception(\Lang::get('message.find-discount-error'));
77
        }
78
    }
79
}
80