8000 minor #36556 Use is_file() instead of file_exists() where possible (n… · Koc/symfony@87c9ab4 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 87c9ab4

Browse files
committed
minor symfony#36556 Use is_file() instead of file_exists() where possible (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- Use is_file() instead of file_exists() where possible | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix symfony#36493 | License | MIT | Doc PR | - Thanks to caching, `is_file()` is much faster than `file_exists()`. I screened the codebase for places where it could matter, here they are. Commits ------- f38904e Use is_file() instead of file_exists() where possible
2 parents 83b37e8 + f38904e commit 87c9ab4

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/ComposerResourc D11B e.php

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)
0