Passed
Push — 4.x ( 651f39...1d49e7 )
by Doug
02:50
created

AddNewConstantsVisitor::enterNode()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 60
rs 7.7724
cc 9
nc 14
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
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\EPSG\Import;
10
11
use function array_merge;
12
use function explode;
13
use function is_string;
14
use PhpParser\Comment\Doc;
15
use PhpParser\Node;
16
use PhpParser\Node\Stmt\Class_;
17
use PhpParser\Node\Stmt\ClassConst;
18
use PhpParser\Node\Stmt\ClassLike;
19
use PhpParser\NodeTraverser;
20
use PhpParser\NodeVisitorAbstract;
21
use function preg_replace;
22
use function str_replace;
23
use function strtoupper;
24
use function trim;
25
use function wordwrap;
26
27
class AddNewConstantsVisitor extends NodeVisitorAbstract
28
{
29
    private array $constants;
30
31
    private string $visibility;
32
33
    public function __construct(array $constants, string $visibility)
34
    {
35
        $this->constants = $constants;
36
        $this->visibility = $visibility;
37
    }
38
39
    public function enterNode(Node $node)
40
    {
41
        if ($node instanceof ClassLike) {
42
            $commentNodes = [];
43
44
            foreach ($this->constants as $row) {
45
                $name = str_replace(
46
                    [' ', '-', '\'', '(', ')', '[', ']', '.', '/', '=', ',', ':', '°', '+', '&', '<>'],
47
                    ['_', '_', '', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_DEG_', '_PLUS_', '_AND_', '_TO_'],
48
                    $row['constant_name']
49
                );
50
                $name = preg_replace('/_+/', '_', $name);
51
                $name = trim($name, '_');
52
53
                $comment = '';
54
                if ($row['constant_help'] || $row['deprecated']) {
55
                    $row['constant_help'] = str_replace(
56
                        [
57
                            'See information source for formula and example.',
58
                            'This is a parameter-less conversion.',
59
                        ],
60
                        [
61
                            '',
62
                            '',
63
                        ],
64
                        $row['constant_help']
65
                    );
66
67
                    $comment .= "/**\n";
68
                    $helpLines = explode("\n", wordwrap($row['constant_help'], 112));
69
                    foreach ($helpLines as $helpLine) {
70
                        $comment .= '* ' . trim($helpLine) . "\n";
71
                    }
72
                    if ($row['deprecated']) {
73
                        $comment .= "* @deprecated\n";
74
                    }
75
                    $comment .= " */\n";
76
                }
77
78
                $constName = strtoupper('EPSG_' . $name);
79
                $constValue = is_string($row['constant_value']) ? new Node\Scalar\String_($row['constant_value']) : new Node\Scalar\LNumber($row['constant_value']);
80
                $constComment = new Doc($comment);
81
                $const = new Node\Const_($constName, $constValue);
82
83
                $flags = Class_::MODIFIER_PUBLIC;
84
                if ($this->visibility === 'protected') {
85
                    $flags = Class_::MODIFIER_PROTECTED;
86
                }
87
88
                $constStmt = new ClassConst([$const], $flags);
89
                $constStmt->setDocComment($constComment);
90
                $commentNodes[] = $constStmt;
91
            }
92
93
            $node->stmts = array_merge($commentNodes, $node->stmts);
94
95
            return NodeTraverser::STOP_TRAVERSAL;
96
        }
97
98
        return null;
99
    }
100
}
101