• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / php-generator / 6017814557

29 Aug 2023 09:39PM UTC coverage: 93.385%. Remained the same
6017814557

push

github

dg
ClassType::class(), interface(), traits() & enum() are deprecated

4 of 4 new or added lines in 1 file covered. (100.0%)

1454 of 1557 relevant lines covered (93.38%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.96
/src/PhpGenerator/Helpers.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\PhpGenerator;
11

12
use Nette;
13

14

15
/**
16
 * @internal
17
 */
18
final class Helpers
19
{
20
        use Nette\StaticClass;
21

22
        public const ReIdentifier = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
23

24
        public const Keywords = [
25
                // built-in types
26
                'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
27
                'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1, 'mixed' => 1, 'false' => 1,
28
                'never' => 1, 'true' => 1,
29

30
                // class keywords
31
                'self' => 1, 'parent' => 1, 'static' => 1,
32

33
                // PHP keywords
34
                'include' => 1, 'include_once' => 1, 'eval' => 1, 'require' => 1, 'require_once' => 1, 'or' => 1, 'xor' => 1,
35
                'and' => 1, 'instanceof' => 1, 'new' => 1, 'clone' => 1, 'exit' => 1, 'if' => 1, 'elseif' => 1, 'else' => 1,
36
                'endif' => 1, 'echo' => 1, 'do' => 1, 'while' => 1, 'endwhile' => 1, 'for' => 1, 'endfor' => 1, 'foreach' => 1,
37
                'endforeach' => 1, 'declare' => 1, 'enddeclare' => 1, 'as' => 1, 'try' => 1, 'catch' => 1, 'finally' => 1,
38
                'throw' => 1, 'use' => 1, 'insteadof' => 1, 'global' => 1, 'var' => 1, 'unset' => 1, 'isset' => 1, 'empty' => 1,
39
                'continue' => 1, 'goto' => 1, 'function' => 1, 'const' => 1, 'return' => 1, 'print' => 1, 'yield' => 1, 'list' => 1,
40
                'switch' => 1, 'endswitch' => 1, 'case' => 1, 'default' => 1, 'break' => 1,
41
                'extends' => 1, 'implements' => 1, 'namespace' => 1, 'trait' => 1, 'interface' => 1, 'class' => 1, '__CLASS__' => 1,
42
                '__TRAIT__' => 1, '__FUNCTION__' => 1, '__METHOD__' => 1, '__LINE__' => 1, '__FILE__' => 1, '__DIR__' => 1,
43
                '__NAMESPACE__' => 1, 'fn' => 1, 'match' => 1, 'enum' => 1, 'abstract' => 1, 'final' => 1,
44
                'private' => 1, 'protected' => 1, 'public' => 1, 'readonly' => 1,
45
        ];
46

47
        /** @deprecated  */
48
        public const
49
                PHP_IDENT = self::ReIdentifier,
50
                KEYWORDS = self::Keywords;
51

52

53
        public static function formatDocComment(string $content, bool $forceMultiLine = false): string
1✔
54
        {
55
                $s = trim($content);
1✔
56
                $s = str_replace('*/', '* /', $s);
1✔
57
                if ($s === '') {
1✔
58
                        return '';
1✔
59
                } elseif ($forceMultiLine || str_contains($content, "\n")) {
1✔
60
                        $s = str_replace("\n", "\n * ", "/**\n$s") . "\n */";
1✔
61
                        return Nette\Utils\Strings::normalize($s) . "\n";
1✔
62
                } else {
63
                        return "/** $s */\n";
1✔
64
                }
65
        }
66

67

68
        public static function tagName(string $name, string $of = PhpNamespace::NameNormal): string
1✔
69
        {
70
                return isset(self::Keywords[strtolower($name)])
1✔
71
                        ? $name
1✔
72
                        : "/*($of*/$name";
1✔
73
        }
74

75

76
        public static function simplifyTaggedNames(string $code, ?PhpNamespace $namespace): string
1✔
77
        {
78
                return preg_replace_callback('~/\*\(([ncf])\*/([\w\x7f-\xff\\\\]++)~', function ($m) use ($namespace) {
1✔
79
                        [, $of, $name] = $m;
1✔
80
                        return $namespace
1✔
81
                                ? $namespace->simplifyType($name, $of)
1✔
82
                                : $name;
1✔
83
                }, $code);
1✔
84
        }
85

86

87
        public static function unformatDocComment(string $comment): string
1✔
88
        {
89
                return preg_replace('#^\s*\* ?#m', '', trim(trim(trim($comment), '/*')));
1✔
90
        }
91

92

93
        public static function unindent(string $s, int $level = 1): string
1✔
94
        {
95
                return $level
1✔
96
                        ? preg_replace('#^(\t| {4}){1,' . $level . '}#m', '', $s)
1✔
97
                        : $s;
1✔
98
        }
99

100

101
        public static function isIdentifier(mixed $value): bool
1✔
102
        {
103
                return is_string($value) && preg_match('#^' . self::ReIdentifier . '$#D', $value);
1✔
104
        }
105

106

107
        public static function isNamespaceIdentifier(mixed $value, bool $allowLeadingSlash = false): bool
1✔
108
        {
109
                $re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::ReIdentifier . '(\\\\' . self::ReIdentifier . ')*$#D';
1✔
110
                return is_string($value) && preg_match($re, $value);
1✔
111
        }
112

113

114
        public static function extractNamespace(string $name): string
1✔
115
        {
116
                return ($pos = strrpos($name, '\\')) ? substr($name, 0, $pos) : '';
1✔
117
        }
118

119

120
        public static function extractShortName(string $name): string
1✔
121
        {
122
                return ($pos = strrpos($name, '\\')) === false
1✔
123
                        ? $name
1✔
124
                        : substr($name, $pos + 1);
1✔
125
        }
126

127

128
        public static function tabsToSpaces(string $s, int $count = 4): string
1✔
129
        {
130
                return str_replace("\t", str_repeat(' ', $count), $s);
1✔
131
        }
132

133

134
        /**
135
         * @param  mixed[]  $props
136
         * @internal
137
         */
138
        public static function createObject(string $class, array $props): object
139
        {
140
                return Dumper::createObject($class, $props);
×
141
        }
142

143

144
        public static function validateType(?string $type, bool &$nullable = false): ?string
1✔
145
        {
146
                if ($type === '' || $type === null) {
1✔
147
                        return null;
1✔
148
                } elseif (!Nette\Utils\Validators::isTypeDeclaration($type)) {
1✔
149
                        throw new Nette\InvalidArgumentException("Value '$type' is not valid type.");
1✔
150
                }
151

152
                if ($type[0] === '?') {
1✔
153
                        $nullable = true;
1✔
154
                        return substr($type, 1);
1✔
155
                }
156

157
                return $type;
1✔
158
        }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc