Use is_file() instead of file_exists() where possible

This commit is contained in:
Nicolas Grekas 2020-04-24 00:29:13 +02:00
parent 83b37e8010
commit f38904ea93
17 changed files with 45 additions and 45 deletions

View File

@ -198,7 +198,7 @@ class KernelBrowser extends HttpKernelBrowser
if (0 === strpos($class, 'ComposerAutoloaderInit')) { if (0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class); $r = new \ReflectionClass($class);
$file = \dirname($r->getFileName(), 2).'/autoload.php'; $file = \dirname($r->getFileName(), 2).'/autoload.php';
if (file_exists($file)) { if (is_file($file)) {
$requires .= 'require_once '.var_export($file, true).";\n"; $requires .= 'require_once '.var_export($file, true).";\n";
} }
} }

View File

@ -38,7 +38,7 @@ class DotenvVault extends AbstractVault
$this->validateName($name); $this->validateName($name);
$v = str_replace("'", "'\\''", $value); $v = str_replace("'", "'\\''", $value);
$content = file_exists($this->dotenvFile) ? file_get_contents($this->dotenvFile) : ''; $content = is_file($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
$content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)/m", "$name='$v'", $content, -1, $count); $content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)/m", "$name='$v'", $content, -1, $count);
if (!$count) { if (!$count) {
@ -70,7 +70,7 @@ class DotenvVault extends AbstractVault
$this->lastMessage = null; $this->lastMessage = null;
$this->validateName($name); $this->validateName($name);
$content = file_exists($this->dotenvFile) ? file_get_contents($this->dotenvFile) : ''; $content = is_file($this->dotenvFile) ? file_get_contents($this->dotenvFile) : '';
$content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)\n?/m", '', $content, -1, $count); $content = preg_replace("/^$name=((\\\\'|'[^']++')++|.*)\n?/m", '', $content, -1, $count);
if ($count) { if ($count) {

View File

@ -58,7 +58,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
// ignore failures to load keys // ignore failures to load keys
} }
if ('' !== $this->decryptionKey && !file_exists($this->pathPrefix.'encrypt.public.php')) { if ('' !== $this->decryptionKey && !is_file($this->pathPrefix.'encrypt.public.php')) {
$this->export('encrypt.public', $this->encryptionKey); $this->export('encrypt.public', $this->encryptionKey);
} }
@ -99,7 +99,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
$this->lastMessage = null; $this->lastMessage = null;
$this->validateName($name); $this->validateName($name);
if (!file_exists($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) { if (!is_file($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
$this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR)); $this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR));
return null; return null;
@ -133,7 +133,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
$this->lastMessage = null; $this->lastMessage = null;
$this->validateName($name); $this->validateName($name);
if (!file_exists($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) { if (!is_file($file = $this->pathPrefix.$name.'.'.substr_replace(md5($name), '.php', -26))) {
$this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR)); $this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath(\dirname($this->pathPrefix).\DIRECTORY_SEPARATOR));
return false; return false;
@ -152,7 +152,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
{ {
$this->lastMessage = null; $this->lastMessage = null;
if (!file_exists($file = $this->pathPrefix.'list.php')) { if (!is_file($file = $this->pathPrefix.'list.php')) {
return []; return [];
} }
@ -184,11 +184,11 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
return; return;
} }
if (file_exists($this->pathPrefix.'decrypt.private.php')) { if (is_file($this->pathPrefix.'decrypt.private.php')) {
$this->decryptionKey = (string) include $this->pathPrefix.'decrypt.private.php'; $this->decryptionKey = (string) include $this->pathPrefix.'decrypt.private.php';
} }
if (file_exists($this->pathPrefix.'encrypt.public.php')) { if (is_file($this->pathPrefix.'encrypt.public.php')) {
$this->encryptionKey = (string) include $this->pathPrefix.'encrypt.public.php'; $this->encryptionKey = (string) include $this->pathPrefix.'encrypt.public.php';
} elseif ('' !== $this->decryptionKey) { } elseif ('' !== $this->decryptionKey) {
$this->encryptionKey = sodium_crypto_box_publickey($this->decryptionKey); $this->encryptionKey = sodium_crypto_box_publickey($this->decryptionKey);

View File

@ -53,7 +53,7 @@ class JsonManifestVersionStrategy implements VersionStrategyInterface
private function getManifestPath(string $path): ?string private function getManifestPath(string $path): ?string
{ {
if (null === $this->manifestData) { if (null === $this->manifestData) {
if (!file_exists($this->manifestPath)) { if (!is_file($this->manifestPath)) {
throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath)); throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath));
} }

View File

@ -65,11 +65,11 @@ class FilesystemTagAwareAdapter extends AbstractTagAwareAdapter implements Prune
} }
for ($i = 0; $i < 38; ++$i) { for ($i = 0; $i < 38; ++$i) {
if (!file_exists($dir.$chars[$i])) { if (!is_dir($dir.$chars[$i])) {
continue; continue;
} }
for ($j = 0; $j < 38; ++$j) { for ($j = 0; $j < 38; ++$j) {
if (!file_exists($d = $dir.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) { if (!is_dir($d = $dir.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
continue; continue;
} }
foreach (scandir($d, SCANDIR_SORT_NONE) ?: [] as $link) { foreach (scandir($d, SCANDIR_SORT_NONE) ?: [] as $link) {
@ -136,7 +136,7 @@ class FilesystemTagAwareAdapter extends AbstractTagAwareAdapter implements Prune
{ {
foreach ($ids as $id) { foreach ($ids as $id) {
$file = $this->getFile($id); $file = $this->getFile($id);
if (!file_exists($file) || !$h = @fopen($file, 'rb')) { if (!is_file($file) || !$h = @fopen($file, 'rb')) {
continue; continue;
} }
@ -193,7 +193,7 @@ class FilesystemTagAwareAdapter extends AbstractTagAwareAdapter implements Prune
protected function doInvalidate(array $tagIds): bool protected function doInvalidate(array $tagIds): bool
{ {
foreach ($tagIds as $tagId) { foreach ($tagIds as $tagId) {
if (!file_exists($tagFolder = $this->getTagFolder($tagId))) { if (!is_dir($tagFolder = $this->getTagFolder($tagId))) {
continue; continue;
} }

View File

@ -390,7 +390,7 @@ EOF;
{ {
if (isset(self::$valuesCache[$this->file])) { if (isset(self::$valuesCache[$this->file])) {
$values = self::$valuesCache[$this->file]; $values = self::$valuesCache[$this->file];
} elseif (!file_exists($this->file)) { } elseif (!is_file($this->file)) {
$this->keys = $this->values = []; $this->keys = $this->values = [];
return; return;

View File

@ -38,7 +38,7 @@ trait FilesystemCommonTrait
} else { } else {
$directory .= \DIRECTORY_SEPARATOR.'@'; $directory .= \DIRECTORY_SEPARATOR.'@';
} }
if (!file_exists($directory)) { if (!is_dir($directory)) {
@mkdir($directory, 0777, true); @mkdir($directory, 0777, true);
} }
$directory .= \DIRECTORY_SEPARATOR; $directory .= \DIRECTORY_SEPARATOR;
@ -77,7 +77,7 @@ trait FilesystemCommonTrait
foreach ($ids as $id) { foreach ($ids as $id) {
$file = $this->getFile($id); $file = $this->getFile($id);
$ok = (!file_exists($file) || $this->doUnlink($file) || !file_exists($file)) && $ok; $ok = (!is_file($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
} }
return $ok; return $ok;
@ -113,7 +113,7 @@ trait FilesystemCommonTrait
$hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true))); $hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
$dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR); $dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
if ($mkdir && !file_exists($dir)) { if ($mkdir && !is_dir($dir)) {
@mkdir($dir, 0777, true); @mkdir($dir, 0777, true);
} }
@ -127,19 +127,19 @@ trait FilesystemCommonTrait
private function scanHashDir(string $directory): \Generator private function scanHashDir(string $directory): \Generator
{ {
if (!file_exists($directory)) { if (!is_dir($directory)) {
return; return;
} }
$chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for ($i = 0; $i < 38; ++$i) { for ($i = 0; $i < 38; ++$i) {
if (!file_exists($directory.$chars[$i])) { if (!is_dir($directory.$chars[$i])) {
continue; continue;
} }
for ($j = 0; $j < 38; ++$j) { for ($j = 0; $j < 38; ++$j) {
if (!file_exists($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) { if (!is_dir($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
continue; continue;
} }
@ -178,7 +178,7 @@ trait FilesystemCommonTrait
if (method_exists(parent::class, '__destruct')) { if (method_exists(parent::class, '__destruct')) {
parent::__destruct(); parent::__destruct();
} }
if (null !== $this->tmp && file_exists($this->tmp)) { if (null !== $this->tmp && is_file($this->tmp)) {
unlink($this->tmp); unlink($this->tmp);
} }
} }

View File

@ -59,7 +59,7 @@ trait FilesystemTrait
foreach ($ids as $id) { foreach ($ids as $id) {
$file = $this->getFile($id); $file = $this->getFile($id);
if (!file_exists($file) || !$h = @fopen($file, 'rb')) { if (!is_file($file) || !$h = @fopen($file, 'rb')) {
continue; continue;
} }
if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) { if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
@ -85,7 +85,7 @@ trait FilesystemTrait
{ {
$file = $this->getFile($id); $file = $this->getFile($id);
return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id])); return is_file($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
} }
/** /**

View File

@ -61,7 +61,7 @@ class ComposerResource implements SelfCheckingResourceInterface
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) { if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class); $r = new \ReflectionClass($class);
$v = \dirname($r->getFileName(), 2); $v = \dirname($r->getFileName(), 2);
if (file_exists($v.'/composer/installed.json')) { if (is_file($v.'/composer/installed.json')) {
self::$runtimeVendors[$v] = @filemtime($v.'/composer/installed.json'); self::$runtimeVendors[$v] = @filemtime($v.'/composer/installed.json');
} }
} }

View File

@ -83,7 +83,7 @@ class ReflectionClassResource implements SelfCheckingResourceInterface
} }
do { do {
$file = $class->getFileName(); $file = $class->getFileName();
if (false !== $file && file_exists($file)) { if (false !== $file && is_file($file)) {
foreach ($this->excludedVendors as $vendor) { foreach ($this->excludedVendors as $vendor) {
if (0 === strpos($file, $vendor) && false !== strpbrk(substr($file, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) { if (0 === strpos($file, $vendor) && false !== strpbrk(substr($file, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
$file = false; $file = false;

View File

@ -114,7 +114,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
if (!is_scalar($file = $getEnv($name))) { if (!is_scalar($file = $getEnv($name))) {
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name)); throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
} }
if (!file_exists($file)) { if (!is_file($file)) {
throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name)); throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name));
} }

View File

@ -670,7 +670,7 @@ class YamlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
} }
if (!file_exists($file)) { if (!is_file($file)) {
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file)); throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
} }

View File

@ -110,7 +110,7 @@ final class Dotenv
{ {
$k = $envKey ?? $this->envKey; $k = $envKey ?? $this->envKey;
if (file_exists($path) || !file_exists($p = "$path.dist")) { if (is_file($path) || !is_file($p = "$path.dist")) {
$this->load($path); $this->load($path);
} else { } else {
$this->load($p); $this->load($p);
@ -120,7 +120,7 @@ final class Dotenv
$this->populate([$k => $env = $defaultEnv]); $this->populate([$k => $env = $defaultEnv]);
} }
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) { if (!\in_array($env, $testEnvs, true) && is_file($p = "$path.local")) {
$this->load($p); $this->load($p);
$env = $_SERVER[$k] ?? $_ENV[$k] ?? $env; $env = $_SERVER[$k] ?? $_ENV[$k] ?? $env;
} }
@ -129,11 +129,11 @@ final class Dotenv
return; return;
} }
if (file_exists($p = "$path.$env")) { if (is_file($p = "$path.$env")) {
$this->load($p); $this->load($p);
} }
if (file_exists($p = "$path.$env.local")) { if (is_file($p = "$path.$env.local")) {
$this->load($p); $this->load($p);
} }
} }
@ -148,7 +148,7 @@ final class Dotenv
public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test']): void public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test']): void
{ {
$p = $path.'.local.php'; $p = $path.'.local.php';
$env = (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($p)) || file_exists($p) ? include $p : null; $env = is_file($p) ? include $p : null;
$k = $this->envKey; $k = $this->envKey;
if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) { if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {

View File

@ -185,7 +185,7 @@ class DebugClassLoader
]; ];
if (!isset(self::$caseCheck)) { if (!isset(self::$caseCheck)) {
$file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), \DIRECTORY_SEPARATOR); $file = is_file(__FILE__) ? __FILE__ : rtrim(realpath('.'), \DIRECTORY_SEPARATOR);
$i = strrpos($file, \DIRECTORY_SEPARATOR); $i = strrpos($file, \DIRECTORY_SEPARATOR);
$dir = substr($file, 0, 1 + $i); $dir = substr($file, 0, 1 + $i);
$file = substr($file, 1 + $i); $file = substr($file, 1 + $i);
@ -904,7 +904,7 @@ class DebugClassLoader
static $patchedMethods = []; static $patchedMethods = [];
static $useStatements = []; static $useStatements = [];
if (!file_exists($file = $method->getFileName()) || isset($patchedMethods[$file][$startLine = $method->getStartLine()])) { if (!is_file($file = $method->getFileName()) || isset($patchedMethods[$file][$startLine = $method->getStartLine()])) {
return; return;
} }
@ -1002,7 +1002,7 @@ EOTXT;
$useMap = []; $useMap = [];
$useOffset = 0; $useOffset = 0;
if (!file_exists($file)) { if (!is_file($file)) {
return [$namespace, $useOffset, $useMap]; return [$namespace, $useOffset, $useMap];
} }
@ -1045,7 +1045,7 @@ EOTXT;
return; return;
} }
if (!file_exists($file = $method->getFileName())) { if (!is_file($file = $method->getFileName())) {
return; return;
} }

View File

@ -280,12 +280,12 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
if (null === $this->projectDir) { if (null === $this->projectDir) {
$r = new \ReflectionObject($this); $r = new \ReflectionObject($this);
if (!file_exists($dir = $r->getFileName())) { if (!is_file($dir = $r->getFileName())) {
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name)); throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
} }
$dir = $rootDir = \dirname($dir); $dir = $rootDir = \dirname($dir);
while (!file_exists($dir.'/composer.json')) { while (!is_file($dir.'/composer.json')) {
if ($dir === \dirname($dir)) { if ($dir === \dirname($dir)) {
return $this->projectDir = $rootDir; return $this->projectDir = $rootDir;
} }
@ -432,7 +432,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
$errorLevel = error_reporting(E_ALL ^ E_WARNING); $errorLevel = error_reporting(E_ALL ^ E_WARNING);
try { try {
if (file_exists($cachePath) && \is_object($this->container = include $cachePath) if (is_file($cachePath) && \is_object($this->container = include $cachePath)
&& (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh())) && (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh()))
) { ) {
self::$freshCache[$cachePath] = true; self::$freshCache[$cachePath] = true;

View File

@ -212,7 +212,7 @@ class ExceptionCaster
$ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0; $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
$ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0; $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
if (file_exists($f['file']) && 0 <= self::$srcContext) { if (is_file($f['file']) && 0 <= self::$srcContext) {
if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) { if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
$template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class'])); $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
@ -220,7 +220,7 @@ class ExceptionCaster
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : ''); $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
$templateInfo = $template->getDebugInfo(); $templateInfo = $template->getDebugInfo();
if (isset($templateInfo[$f['line']])) { if (isset($templateInfo[$f['line']])) {
if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) { if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) {
$templatePath = null; $templatePath = null;
} }
if ($templateSrc) { if ($templateSrc) {

View File

@ -43,7 +43,7 @@ class LinkStub extends ConstStub
return; return;
} }
if (!file_exists($href)) { if (!is_file($href)) {
return; return;
} }
if ($line) { if ($line) {
@ -72,7 +72,7 @@ class LinkStub extends ConstStub
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) { if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class); $r = new \ReflectionClass($class);
$v = \dirname($r->getFileName(), 2); $v = \dirname($r->getFileName(), 2);
if (file_exists($v.'/composer/installed.json')) { if (is_file($v.'/composer/installed.json')) {
self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR; self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR;
} }
} }
@ -91,7 +91,7 @@ class LinkStub extends ConstStub
} }
$parent = $dir; $parent = $dir;
while (!@file_exists($parent.'/composer.json')) { while (!@is_file($parent.'/composer.json')) {
if (!@file_exists($parent)) { if (!@file_exists($parent)) {
// open_basedir restriction in effect // open_basedir restriction in effect
break; break;