Passed
Push — master ( 5545f1...83deac )
by Kirill
03:22
created

ReplaceTest::testReplace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 110
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 32
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 110
rs 9.408

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
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Reactor;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Reactor\ClassDeclaration;
16
use Spiral\Reactor\FileDeclaration;
17
use Spiral\Reactor\NamespaceDeclaration;
18
use Spiral\Reactor\Partial;
19
20
class ReplaceTest extends TestCase
21
{
22
    public function testReplace(): void
23
    {
24
        $declaration = new ClassDeclaration('MyClass');
25
        $declaration->setExtends('Record');
26
27
        $declaration->property('names')
28
            ->setAccess(Partial\Property::ACCESS_PRIVATE)
29
            ->setComment(['This is foxes', '', '@var array'])
30
            ->setDefaultValue(['name' => 11, 'value' => 'hi', 'test' => []]);
31
32
        $method = $declaration->method('sample');
33
        $method->parameter('input')->setType('int');
34
        $method->parameter('output')->setType('int')->setDefaultValue(null)->setPBR(true);
35
        $method->setAccess(Partial\Method::ACCESS_PUBLIC)->setStatic(true);
36
        $method->setComment('Get some foxes');
37
38
        $method->setSource([
39
            '$output = $input;',
40
            'return true;'
41
        ]);
42
43
        $declaration->addTrait('Spiral\Debug\Traits\LoggerTrait');
44
        $this->assertTrue($declaration->hasTrait('Spiral\Debug\Traits\LoggerTrait'));
45
46
        $namespace = new NamespaceDeclaration('Namespace');
47
        $namespace->setComment('All about foxes');
48
49
        $namespace->addElement($declaration);
50
51
        $file = new FileDeclaration();
52
        $file->getComment()->addLine('Full file of foxes');
53
        $file->addElement($namespace);
54
55
        $this->assertSame(
56
            preg_replace('/\s+/', '', '<?php
57
            /**
58
             * Full file of foxes
59
             */
60
            /**
61
             * All about foxes
62
             */
63
            namespace Namespace {
64
                class MyClass extends Record
65
                {
66
                    use Spiral\Debug\Traits\LoggerTrait;
67
68
                    /**
69
                     * This is foxes
70
                     *
71
                     * @var array
72
                     */
73
                    private $names = [
74
                        \'name\'  => 11,
75
                        \'value\' => \'hi\',
76
                        \'test\'  => []
77
                    ];
78
79
                    /**
80
                     * Get some foxes
81
                     */
82
                    public static function sample(int $input, int &$output = null)
83
                    {
84
                        $output = $input;
85
                        return true;
86
                    }
87
                }
88
            }'),
89
            preg_replace('/\s+/', '', $file->render())
90
        );
91
92
        $file->replace('foxes', 'dogs');
93
94
        $this->assertSame(
95
            preg_replace('/\s+/', '', '<?php
96
                /**
97
                 * Full file of dogs
98
                 */
99
                /**
100
                 * All about dogs
101
                 */
102
                namespace Namespace {
103
                    class MyClass extends Record
104
                    {
105
                        use Spiral\Debug\Traits\LoggerTrait;
106
107
                        /**
108
                         * This is dogs
109
                         *
110
                         * @var array
111
                         */
112
                        private $names = [
113
                            \'name\'  => 11,
114
                            \'value\' => \'hi\',
115
                            \'test\'  => []
116
                        ];
117
118
                        /**
119
                         * Get some dogs
120
                         */
121
                        public static function sample(int $input, int &$output = null)
122
                        {
123
                            $output = $input;
124
                            return true;
125
                        }
126
                    }
127
                }'),
128
            preg_replace(
129
                '/\s+/',
130
                '',
131
                $file->render()
132
            )
133
        );
134
    }
135
}
136