Completed
Pull Request — master (#93)
by Bart
06:30
created

Sources   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMappedSources() 0 12 3
C getSource() 0 57 16
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\BaseApplicationComponent as BaseApplication;
7
8
/**
9
 * Schematic Sources Service.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2017, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @link      http://www.nerds.company
18
 */
19
class Sources extends BaseApplication
20
{
21
    /**
22
    * Get sources based on the indexFrom attribute and return them with the indexTo attribute.
23
    *
24
    * @param string       $fieldType
25
    * @param string|array $sources
26
    * @param string       $indexFrom
27
    * @param string       $indexTo
28
    *
29
    * @return array|string
30
    */
31
   public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
32
   {
33
       $mappedSources = $sources;
34
       if (is_array($sources)) {
35
           $mappedSources = [];
36
           foreach ($sources as $source) {
37
               $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
38
           }
39
       }
40
41
       return $mappedSources;
42
   }
43
44
   /**
45
    * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
46
    *
47
    * @TODO Break up and simplify this method
48
    *
49
    * @param string $fieldType
50
    * @param string $source
51
    * @param string $indexFrom
52
    * @param string $indexTo
53
    *
54
    * @return string
55
    */
56
   public function getSource($fieldType, $source, $indexFrom, $indexTo)
57
   {
58
       if ($source == 'singles' || $source == '*') {
59
           return $source;
60
       }
61
62
      /** @var BaseElementModel $sourceObject */
63
      $sourceObject = null;
64
65
       if (strpos($source, ':') > -1) {
66
           list($sourceType, $sourceFrom) = explode(':', $source);
67
           switch ($sourceType) {
68
              case 'section':
69
                  $service = Craft::app()->sections;
70
                  $method = 'getSectionBy';
71
                  break;
72
              case 'group':
73
                  $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
74
                  $method = 'getGroupBy';
75
                  break;
76
              case 'folder':
77
                  $service = Craft::app()->schematic_assetSources;
78
                  $method = 'getSourceBy';
79
                  break;
80
              case 'taggroup':
81
                  $service = Craft::app()->tags;
82
                  $method = 'getTagGroupBy';
83
                  break;
84
              case 'field':
85
                  $service = Craft::app()->fields;
86
                  $method = 'getFieldBy';
87
                  break;
88
          }
89
       } elseif ($source !== 'singles') {
90
           //Backwards compatibility
91
          $sourceType = 'section';
92
           $sourceFrom = $source;
93
           $service = Craft::app()->sections;
94
           $method = 'getSectionBy';
95
       }
96
       var_dump($source);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($source); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
97
       var_dump($service);
0 ignored issues
show
Bug introduced by
The variable $service 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...
98
       var_dump($method);
0 ignored issues
show
Bug introduced by
The variable $method 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...
99
       var_dump($sourceFrom);
0 ignored issues
show
Bug introduced by
The variable $sourceFrom 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...
100
101
       if (isset($service) && isset($method) && isset($sourceFrom)) {
102
           $method = $method.$indexFrom;
103
           $sourceObject = $service->$method($sourceFrom);
104
           var_dump($sourceObject);
105
       }
106
107
       if ($sourceObject && isset($sourceType)) {
108
           return $sourceType.':'.$sourceObject->$indexTo;
109
       }
110
111
       return $source;
112
   }
113
}
114