8000 Use is_file() instead of file_exists() where possible · Koc/symfony@f38904e · GitHub
[go: up one dir, main page]

Skip to content

Commit f38904e

Browse files
Use is_file() instead of file_exists() where possible
1 parent 83b37e8 commit f38904e

File tree

17 files changed

+45
-45
lines changed

17 files changed

+45
-45
lines changed

src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected function getScript($request)
198198
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
199199
$r = new \ReflectionClass($class);
200200
$file = \dirname($r->getFileName(), 2).'/autoload.php';
201-
if (file_exists($file)) {
201+
if (is_file($file)) {
202202
$requires .= 'require_once '.var_export($file, true).";\n";
203203
}
204204
}

src/Symfony/Bundle/FrameworkBundle/Secrets/DotenvVault.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function seal(string $name, string $value): void
3838
$this->validateName($name);
3939
$v = str_replace("'", "'\\''", $value);
4040

41-
$content = file_exists($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
41+
$content = is_file($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
4242
$content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)/m", "$name='$v'", $content, -1, $count);
4343

4444
if (!$count) {
@@ -70,7 +70,7 @@ public function remove(string $name): bool
7070
$this->lastMessage = null;
7171
$this->validateName($name);
7272

73-
$content = file_exists($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
73+
$content = is_file($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
7474
$content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)\n?/m", '', $content, -1, $count);
7575

7676
if ($count) {

src/Symfony/Bundle/FrameworkBundle/Secrets/SodiumVault.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function generateKeys(bool $override = false): bool
5858
// ignore failures to load keys
5959
}
6060

61-
if ('' !== $this->decryptionKey && !file_exists($this->pathPrefix.'encrypt.public.php')) {
61+
if ('' !== $this->decryptionKey && !is_file($this->pathPrefix.'encrypt.public.php')) {
6262
$this->export('encrypt.public', $this->encryptionKey);
6363
}
6464

@@ -99,7 +99,7 @@ public function reveal(string $name): ?string
9999
$this->lastMessage = null;
100100
$this->validateName($name);
101101

102-
if (!file_exists($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
102+
if (!is_file($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
103103
$this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR));
104104

105105
return null;
@@ -133,7 +133,7 @@ public function remove(string $name): bool
133133
$this->lastMessage = null;
134134
$this->validateName($name);
135135

136-
if (!file_exists($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
136+
if (!is_file($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
137137
$this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR));
138138

139139
return false;
@@ -152,7 +152,7 @@ public function list(bool $reveal = false): array
152152
{
153153
$this->lastMessage = null;
154154

155-
if (!file_exists($file = $this->pathPrefix.'list.php')) {
155+
if (!is_file($file = $this->pathPrefix.'list.php')) {
156156
return [];
157157
}
158158

@@ -184,11 +184,11 @@ private function loadKeys(): void
184184
return;
185185
}
186186

187-
if (file_exists($this->pathPrefix.'decrypt.private.php')) {
187+
if (is_file($this->pathPrefix.'decrypt.private.php')) {
188188
$this->decryptionKey = (string) include $this->pathPrefix.'decrypt.private.php';
189189
}
190190

191-
if (file_exists($this->pathPrefix.'encrypt.public.php')) {
191+
if (is_file($this->pathPrefix.'encrypt.public.php')) {
192192
$this->encryptionKey = (string) include $this->pathPrefix.'encrypt.public.php';
193193
} elseif ('' !== $this->decryptionKey) {
194194
$this->encryptionKey = sodium_crypto_box_publickey($this->decryptionKey);

src/Symfony/Component/Asset/VersionStrategy/JsonManifestVersionStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function applyVersion(string $path)
5353
private function getManifestPath(string $path): ?string
5454
{
5555
if (null === $this->manifestData) {
56-
if (!file_exists($this->manifestPath)) {
56+
if (!is_file($this->manifestPath)) {
5757
throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath));
5858
}
5959

src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ protected function doClear(string $namespace)
6565
}
6666

6767
for ($i = 0; $i < 38; ++$i) {
68-
if (!file_exists($dir.$chars[$i])) {
68+
if (!is_dir($dir.$chars[$i])) {
6969
continue;
7070
}
7171
for ($j = 0; $j < 38; ++$j) {
72-
if (!file_exists($d = $dir.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
72+
if (!is_dir($d = $dir.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
7373
continue;
7474
}
7575
foreach (scandir($d, SCANDIR_SORT_NONE) ?: [] as $link) {
@@ -136,7 +136,7 @@ protected function doDeleteYieldTags(array $ids): iterable
136136
{
137137
foreach ($ids as $id) {
138138
$file = $this->getFile($id);
139-
if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
139+
if (!is_file($file) || !$h = @fopen($file, 'rb')) {
140140
continue;
141141
}
142142

@@ -193,7 +193,7 @@ protected function doDeleteTagRelations(array $tagData): bool
193193
protected function doInvalidate(array $tagIds): bool
194194
{
195195
foreach ($tagIds as $tagId) {
196-
if (!file_exists($tagFolder = $this->getTagFolder($tagId))) {
196+
if (!is_dir($tagFolder = $this->getTagFolder($tagId))) {
197197
continue;
198198
}
199199

src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private function initialize()
390390
{
391391
if (isset(self::$valuesCache[$this->file])) {
392392
$values = self::$valuesCache[$this->file];
393-
} elseif (!file_exists($this->file)) {
393+
} elseif (!is_file($this->file)) {
394394
$this->keys = $this->values = [];
395395

396396
return;

src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function init(string $namespace, ?string $directory)
3838
} else {
3939
$directory .= \DIRECTORY_SEPARATOR.'@';
4040
}
41-
if (!file_exists($directory)) {
41+
if (!is_dir($directory)) {
4242
@mkdir($directory, 0777, true);
4343
}
4444
$directory .= \DIRECTORY_SEPARATOR;
@@ -77,7 +77,7 @@ protected function doDelete(array $ids)
7777

7878
foreach ($ids as $id) {
7979
$file = $this->getFile($id);
80-
$ok = (!file_exists($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
80+
$ok = (!is_file($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
8181
}
8282

8383
return $ok;
@@ -113,7 +113,7 @@ private function getFile(string $id, bool $mkdir = false, string $directory = nu
113113
$hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
114114
$dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
115115

116-
if ($mkdir && !file_exists($dir)) {
116+
if ($mkdir && !is_dir($dir)) {
117117
@mkdir($dir, 0777, true);
118118
}
119119

@@ -127,19 +127,19 @@ private function getFileKey(string $file): string
127127

128128
private function scanHashDir(string $directory): \Generator
129129
{
130-
if (!file_exists($directory)) {
130+
if (!is_dir($directory)) {
131131
return;
132132
}
133133

134134
$chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
135135

136136
for ($i = 0; $i < 38; ++$i) {
137-
if (!file_exists($directory.$chars[$i])) {
137+
if (!is_dir($directory.$chars[$i])) {
138138
continue;
139139
}
140140

141141
for ($j = 0; $j < 38; ++$j) {
142-
if (!file_exists($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
142+
if (!is_dir($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
143143
continue;
144144
}
145145

@@ -178,7 +178,7 @@ public function __destruct()
178178
if (method_exists(parent::class, '__destruct')) {
179179
parent::__destruct();
180180
}
181-
if (null !== $this->tmp && file_exists($this->tmp)) {
181+
if (null !== $this->tmp && is_file($this->tmp)) {
182182
unlink($this->tmp);
183183
}
184184
}

src/Symfony/Component/Cache/Traits/FilesystemTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function doFetch(array $ids)
5959

6060
foreach ($ids as $id) {
6161
$file = $this->getFile($id);
62-
if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
62+
if (!is_file($file) || !$h = @fopen($file, 'rb')) {
6363
continue;
6464
}
6565
if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
@@ -85,7 +85,7 @@ protected function doHave(string $id)
8585
{
8686
$file = $this->getFile($id);
8787

88-
return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
88+
return is_file($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
8989
}
9090

9191
/**

src/Symfony/Component/Config/Resource/ComposerResource.php

Lines changed: 1 addition & 1 de 10000 letion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static function refresh()
6161
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
6262
$r = new \ReflectionClass($class);
6363
$v = \dirname($r->getFileName(), 2);
64-
if (file_exists($v.'/composer/installed.json')) {
64+
if (is_file($v.'/composer/installed.json')) {
6565
self::$runtimeVendors[$v] = @filemtime($v.'/composer/installed.json');
6666
}
6767
}

src/Symfony/Component/Config/Resource/ReflectionClassResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function loadFiles(\ReflectionClass $class)
8383
}
8484
do {
8585
$file = $class->getFileName();
86-
if (false !== $file && file_exists($file)) {
86+
if (false !== $file && is_file($file)) {
8787
foreach ($this->excludedVendors as $vendor) {
8888
if (0 === strpos($file, $vendor) && false !== strpbrk(substr($file, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
8989
$file = false;

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
114114
if (!is_scalar($file = $getEnv($name))) {
115115
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
116116
}
117-
if (!file_exists($file)) {
117+
if (!is_file($file)) {
118118
throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name));
119119
}
120120

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ protected function loadFile($file)
670670
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
671671
}
672672

673-
if (!file_exists($file)) {
673+
if (!is_file($file)) {
674674
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
675675
}
676676

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
110110
{
111111
$k = $envKey ?? $this->envKey;
112112

113-
if (file_exists($path) || !file_exists($p = "$path.dist")) {
113+
if (is_file($path) || !is_file($p = "$path.dist")) {
114114
$this->load($path);
115115
} else {
116116
$this->load($p);
@@ -120,7 +120,7 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
120120
$this->populate([$k => $env = $defaultEnv]);
121121
}
122122

123-
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
123+
if (!\in_array($env, $testEnvs, true) && is_file($p = "$path.local")) {
124124
$this->load($p);
125125
$env = $_SERVER[$k] ?? $_ENV[$k] ?? $env;
126126
}
@@ -129,11 +129,11 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
129129
return;
130130
}
131131

132-
if (file_exists($p = "$path.$env")) {
132+
if (is_file($p = "$path.$env")) {
133133
$this->load($p);
134134
}
135135

136-
if (file_exists($p = "$path.$env.local")) {
136+
if (is_file($p = "$path.$env.local")) {
137137
$this->load($p);
138138
}
139139
}
@@ -148,7 +148,7 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
148148
public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test']): void
149149
{
150150
$p = $path.'.local.php';
151-
$env = (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($p)) || file_exists($p) ? include $p : null;
151+
$env = is_file($p) ? include $p : null;
152152
$k = $this->envKey;
153153

154154
if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function __construct(callable $classLoader)
185185
];
186186

187187
if (!isset(self::$caseCheck)) {
188-
$file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), \DIRECTORY_SEPARATOR);
188+
$file = is_file(__FILE__) ? __FILE__ : rtrim(realpath('.'), \DIRECTORY_SEPARATOR);
189189
$i = strrpos($file, \DIRECTORY_SEPARATOR);
190190
$dir = substr($file, 0, 1 + $i);
191191
$file = substr($file, 1 + $i);
@@ -904,7 +904,7 @@ private function patchMethod(\ReflectionMethod $method, string $returnType, stri
904904
static $patchedMethods = [];
905905
static $useStatements = [];
906906

907-
if (!file_exists($file = $method->getFileName()) || isset($patchedMethods[$file][$startLine = $method->getStartLine()])) {
907+
if (!is_file($file = $method->getFileName()) || isset($patchedMethods[$file][$startLine = $method->getStartLine()])) {
908908
return;
909909
}
910910

@@ -1002,7 +1002,7 @@ private static function getUseStatements(string $file): array
10021002
$useMap = [];
10031003
$useOffset = 0;
10041004

1005-
if (!file_exists($file)) {
1005+
if (!is_file($file)) {
10061006
return [$namespace, $useOffset, $useMap];
10071007
}
10081008

@@ -1045,7 +1045,7 @@ private function fixReturnStatements(\ReflectionMethod $method, string $returnTy
10451045
return;
10461046
}
10471047

1048-
if (!file_exists($file = $method->getFileName())) {
1048+
if (!is_file($file = $method->getFileName())) {
10491049
return;
10501050
}
10511051

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,12 @@ public function getProjectDir()
280280
if (null === $this->projectDir) {
281281
$r = new \ReflectionObject($this);
282282

283-
if (!file_exists($dir = $r->getFileName())) {
283+
if (!is_file($dir = $r->getFileName())) {
284284
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
285285
}
286286

287287
$dir = $rootDir = \dirname($dir);
288-
while (!file_exists($dir.'/composer.json')) {
288+
while (!is_file($dir.'/composer.json')) {
289289
if ($dir === \dirname($dir)) {
290290
return $this->projectDir = $rootDir;
291291
}
@@ -432,7 +432,7 @@ protected function initializeContainer()
432432
$errorLevel = error_reporting(E_ALL ^ E_WARNING);
433433

434434
try {
435-
if (file_exists($cachePath) && \is_object($this->container = include $cachePath)
435+
if (is_file($cachePath) && \is_object($this->container = include $cachePath)
436436
&& (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh()))
437437
) {
438438
self::$freshCache[$cachePath] = true;

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, boo
212212
$ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
213213
$ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
214214

215-
if (file_exists($f['file']) && 0 <= self::$srcContext) {
215+
if (is_file($f['file']) && 0 <= self::$srcContext) {
216216
if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
217217
$template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
218218

219219
$ellipsis = 0;
220220
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
221221
$templateInfo = $template->getDebugInfo();
222222
if (isset($templateInfo[$f['line']])) {
223-
if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
223+
if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) {
224224
$templatePath = null;
225225
}
226226
if ($templateSrc) {

src/Symfony/Component/VarDumper/Caster/LinkStub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct($label, int $line = 0, $href = null)
4343

4444
return;
4545
}
46-
if (!file_exists($href)) {
46+
if (!is_file($href)) {
4747
return;
4848
}
4949
if ($line) {
@@ -72,7 +72,7 @@ private function getComposerRoot(string $file, bool &$inVendor)
7272
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
7373
$r = new \ReflectionClass($class);
7474
$v = \dirname($r->getFileName(), 2);
75-
if (file_exists($v.'/composer/installed.json')) {
75+
if (is_file($v.'/composer/installed.json')) {
7676
self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR;
7777
}
7878
}
@@ -91,7 +91,7 @@ private function getComposerRoot(string $file, bool &$inVendor)
9191
}
9292

9393
$parent = $dir;
94-
while (!@file_exists($parent.'/composer.json')) {
94+
while (!@is_file($parent.'/composer.json')) {
9595
if (!@file_exists($parent)) {
9696
// open_basedir restriction in effect
9797
break;

0 commit comments

Comments
 (0)
0