Completed
Push — master ( 205cf7...842449 )
by Arnold
02:56
created

TestHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 11
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
getExtension() 0 1 ?
A buildEnv() 11 11 1
A render() 0 7 1
A assertRender() 0 6 1

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 Jasny\Twig;
4
5
/**
6
 * Helper methods for unit tests
7
 */
8
trait TestHelper
9
{
10
    /**
11
     * Get the tested extension
12
     * 
13
     * @return \Twig_Extension
14
     */
15
    abstract protected function getExtension();
16
    
17
    /**
18
     * Build the Twig environment for the template
19
     * 
20
     * @param string $template
21
     * @return \Twig_Environment
22
     */
23 View Code Duplication
    protected function buildEnv($template)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
24
    {
25
        $loader = new \Twig_Loader_Array([
26
            'template' => $template,
27
        ]);
28
        $twig = new \Twig_Environment($loader);
29
        
30
        $twig->addExtension($this->getExtension());
31
        
32
        return $twig;
33
    }
34
    
35
    /**
36
     * Render template
37
     * 
38
     * @param string $template
39
     * @param array $data
40
     * @return string
41
     */
42
    protected function render($template, $data = [])
43
    {
44
        $twig = $this->buildEnv($template);
45
        $result = $twig->render('template', $data);
46
        
47
        return $result;
48
    }
49
    
50
    /**
51
     * Render template and assert equals
52
     * 
53
     * @param string $expected
54
     * @param string $template
55
     */
56
    protected function assertRender($expected, $template)
57
    {
58
        $result = $this->render($template);
59
        
60
        $this->assertEquals($expected, (string)$result);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
    }
62
}
63