Completed
Push — master ( 0697fc...3b42aa )
by Greg
02:06
created

DataProvider::before()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.1199
c 0
b 0
f 0
cc 8
eloc 40
nc 19
nop 1

How to fix   Long Method   

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
 * Before suite hook that provide dataprovider annotation
4
 * for datadriven tests using non-static data source
5
 */
6
namespace Codeception\Extension;
7
8
use Codeception\Util\Annotation;
9
use Codeception\Step\Comment;
10
use Codeception\Test\Cest as CestFormat;
11
use Codeception\Exception\TestParseException;
12
13
class DataProvider extends \Codeception\Platform\Extension
14
{
15
  // list events to listen to
16
  public static $events = array(
17
    //run before any test`
18
    'suite.before' => 'before'
19
  );
20
21
  public function before(\Codeception\Event\SuiteEvent $e)
22
  {
23
    $suite = $e->getSuite();
0 ignored issues
show
Bug introduced by
The method getSuite does only exist in Codeception\Event\SuiteEvent, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
24
    $tests = $suite->tests();
25
    foreach ($tests as $id => $test) {
26
        if (get_class($test) == 'Codeception\Test\Cest') {
27
            $testClass = $test->getTestClass();
28
            $testMethod = $test->getTestMethod();
29
            $testFile = $test->getMetadata()->getFilename();
30
            $testActor = $test->getMetadata()->getCurrent('actor');
31
            $dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider');
32
            if (false === empty($dataMethod)) {
33
                if (false === is_callable([$testClass, $testMethod])) {
34
                    throw new TestParseException(
35
                        $testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable"
36
                        . PHP_EOL .
37
                        "Make sure this is a public static function."
38
                    );
39
                }
40
                try {
41
                    $dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
42
                    $examples = $testClass::$dataMethod();
43
                    foreach ($examples as $example) {
44
                        if ($example === null) {
45
                            throw new TestParseException(
46
                                $testFile, "Values return by DataProvider for ${testClass}->${testMethod} is invalid"
47
                            );
48
                        }
49
                        $dataTest = new CestFormat($testClass, $testMethod, $testFile);
50
                        $dataTest->getMetadata()->setServices([
51
                            'di'         => $test->getMetadata()->getService('di'),
52
                            'dispatcher' => $test->getMetadata()->getService('dispatcher'),
53
                            'modules'    => $test->getMetadata()->getService('modules')
54
                        ]);
55
                        $dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]);
56
                        $step = new Comment('', $dataTest->getMetadata()->getCurrent('example'));
57
                        $dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | '. $step->getArgumentsAsString(100));
58
                        $groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group');
59
                        $dataProvider->addTest($dataTest, $groups);
60
                    }
61
                    $tests[$id] = $dataProvider;
62
                } catch(\Exception $e) {
63
                    throw new TestParseException(
64
                        $testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable"
65
                        . PHP_EOL .
66
                        "Make sure this is a public static function."
67
                    );
68
                }
69
            }
70
        }
71
    }
72
    $suite->setTests($tests);
73
  }
74
75
}
76