1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\CS\Fixer\PSR0;
use Symfony\CS\AbstractFixer;
use Symfony\CS\ConfigAwareInterface;
use Symfony\CS\ConfigInterface;
use Symfony\CS\StdinFileInfo;
use Symfony\CS\Tokenizer\Tokens;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Bram Gotink <bram@gotink.me>
*/
class Psr0Fixer extends AbstractFixer implements ConfigAwareInterface
{
protected $config;
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);
$namespace = false;
$namespaceIndex = 0;
$namespaceEndIndex = 0;
$classyName = null;
$classyIndex = 0;
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(T_NAMESPACE)) {
if (false !== $namespace) {
return $content;
}
$namespaceIndex = $tokens->getNextMeaningfulToken($index);
$namespaceEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
$namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
} elseif ($token->isClassy()) {
if (null !== $classyName) {
return $content;
}
$classyIndex = $tokens->getNextMeaningfulToken($index);
$classyName = $tokens[$classyIndex]->getContent();
}
}
if (null === $classyName) {
return $content;
}
if (false !== $namespace) {
$normNamespace = str_replace('\\', '/', $namespace);
$path = str_replace('\\', '/', $file->getRealPath());
$dir = dirname($path);
if ($this->config) {
$dir = substr($dir, strlen(realpath($this->config->getDir())) + 1);
if (strlen($normNamespace) > strlen($dir)) {
if ('' !== $dir) {
$normNamespace = substr($normNamespace, -strlen($dir));
} else {
$normNamespace = '';
}
}
}
$dir = substr($dir, -strlen($normNamespace));
if (false === $dir) {
$dir = '';
}
$filename = basename($path, '.php');
if ($classyName !== $filename) {
$tokens[$classyIndex]->setContent($filename);
}
if ($normNamespace !== $dir && strtolower($normNamespace) === strtolower($dir)) {
for ($i = $namespaceIndex; $i <= $namespaceEndIndex; ++$i) {
$tokens[$i]->clear();
}
$namespace = substr($namespace, 0, -strlen($dir)).str_replace('/', '\\', $dir);
$newNamespace = Tokens::fromCode('<?php namespace '.$namespace.';');
$newNamespace[0]->clear();
$newNamespace[1]->clear();
$newNamespace[2]->clear();
$tokens->insertAt($namespaceIndex, $newNamespace);
}
} else {
$normClass = str_replace('_', '/', $classyName);
$path = str_replace('\\', '/', $file->getRealPath());
$filename = substr($path, -strlen($normClass) - 4, -4);
if ($normClass !== $filename && strtolower($normClass) === strtolower($filename)) {
$tokens[$classyIndex]->setContent(str_replace('/', '_', $filename));
}
}
return $tokens->generateCode();
}
/**
* {@inheritdoc}
*/
public function setConfig(ConfigInterface $config)
{
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
return -10;
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
return 'Classes must be in a path that matches their namespace, be at least one namespace deep, and the class name should match the file name.';
}
/**
* {@inheritdoc}
*/
public function supports(\SplFileInfo $file)
{
if ($file instanceof StdinFileInfo) {
return false;
}
$filenameParts = explode('.', $file->getBasename(), 2);
if (
// ignore file with extension other than php
(!isset($filenameParts[1]) || 'php' !== $filenameParts[1])
// ignore file with name that cannot be a class name
|| 0 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $filenameParts[0])
// ignore filename that will halt compiler (and cannot be properly tokenized under PHP 5.3)
|| '__halt_compiler' === $filenameParts[0]
) {
return false;
}
try {
$tokens = Tokens::fromCode(sprintf('<?php class %s {}', $filenameParts[0]));
if ($tokens[3]->isKeyword() || $tokens[3]->isMagicConstant()) {
// name can not be a class name - detected by PHP 5.x
return false;
}
} catch (\ParseError $e) {
// name can not be a class name - detected by PHP 7.x
return false;
}
// ignore stubs/fixtures, since they are typically containing invalid files for various reasons
return !preg_match('{[/\\\\](stub|fixture)s?[/\\\\]}i', $file->getRealPath());
}
}