ronantakizawa/codereview-bench huggingface dataset:
Repository: laravel/framework
File: src/Illuminate/Database/Eloquent/Builder.php (PHP)

Code before:
    public function withoutGlobalScopes(?array $scopes = null)
    {
        if (! is_array($scopes)) {
            $scopes = array_keys($this->scopes);
        }

        foreach ($scopes as $scope) {
            $this->withoutGlobalScope($scope);
        }

        return $this;
    }

    /**
     * Remove all except passed registered global scopes.
     *
     * @param  array  $scopes
     * @return $this
     */
    public function withoutGlobalScopesExcept(array $scopes = [])
    {
        $this->withoutGlobalScopes(
            array_diff(array_keys($this->scopes), $scopes)
        );

        return $this;
    }

    /**
     * Get an array of global scopes that were removed from the query.
     *
     * @return array
     */
    public function removedScopes()
    {
        return $this->removedScopes;
    }

    /**
     * Add a where clause on the primary key to the query.
     *
     * @param  mixed  $id
     * @return $this
     */
    public function whereKey($id)
    {
        if ($id instanceof Model) {
            $id = $id->getKey();
        }

        if (is_array($id) || $id instanceof Arrayable) {

Review comment (suggestion):
You can shorten this with `tap()` if you like:
```suggestion
        return tap($this)->withoutGlobalScopes(
            array_diff(array_keys($this->scopes), $scopes)
        );
```

Code after:
    public function withoutGlobalScopes(?array $scopes = null)
    {
        if (! is_array($scopes)) {
            $scopes = array_keys($this->scopes);
        }

        foreach ($scopes as $scope) {
            $this->withoutGlobalScope($scope);
        }

        return $this;
    }

    /**
     * Remove all global scopes except the given scopes.
     *
     * @param  array  $scopes
     * @return $this
     */
    public function withoutGlobalScopesExcept(array $scopes = [])
    {
        $this->withoutGlobalScopes(
            array_diff(array_keys($this->scopes), $scopes)
        );

        return $this;
    }

    /**
     * Get an array of global scopes that were removed from the query.
     *
     * @return array
     */
    public function removedScopes()
    {
        return $this->removedScopes;
    }

    /**
     * Add a where clause on the primary key to the query.
     *
     * @param  mixed  $id
     * @return $this
     */
    public function whereKey($id)
    {
        if ($id instanceof Model) {
            $id = $id->getKey();
        }

        if (is_array($id) || $id instanceof Arrayable) {
Repository: laravel/framework
File: src/Illuminate/Foundation/resources/exceptions/renderer/components/request-header.blade.php (PHP)

Code before:
@props(['headers'])

<div class="flex flex-col gap-3">
    <h2 class="text-lg font-semibold text-neutral-900 dark:text-white">Headers</h2>
    <div class="flex flex-col">
        @foreach ($headers as $key => $value)
        <div class="flex max-w-full items-baseline gap-2 h-10 text-sm font-mono">
            <div class="uppercase text-neutral-500 dark:text-neutral-400 shrink-0">{{ $key }}</div>
            <div class="min-w-6 grow h-3 border-b-2 border-dotted border-neutral-300 dark:border-white/20"></div>
            <div class="truncate text-neutral-900 dark:text-white">
            <span data-tippy-content="{!! nl2br(e($value)) !!}">
                    {{ $value }}
                </span>
            </div>
        </div>
        @endforeach
    </div>
</div>


Review comment (suggestion):
```suggestion
                <span data-tippy-content="{!! nl2br(e($value)) !!}">
```

Code after:
@props(['headers'])

<div class="flex flex-col gap-3">
    <h2 class="text-lg font-semibold text-neutral-900 dark:text-white">Headers</h2>
    <div class="flex flex-col">
        @foreach ($headers as $key => $value)
        <div class="flex max-w-full items-baseline gap-2 h-10 text-sm font-mono">
            <div class="uppercase text-neutral-500 dark:text-neutral-400 shrink-0">{{ $key }}</div>
            <div class="min-w-6 grow h-3 border-b-2 border-dotted border-neutral-300 dark:border-white/20"></div>
            <div class="truncate text-neutral-900 dark:text-white">
                <span data-tippy-content="{{ $value }}">
                    {{ $value }}
                </span>
            </div>
        </div>
        @endforeach
    </div>
</div>
Repository: laravel/framework
File: src/Illuminate/Foundation/resources/exceptions/renderer/components/request-header.blade.php (PHP)

Code before:
@props(['headers'])

<div class="flex flex-col gap-3">
    <h2 class="text-lg font-semibold text-neutral-900 dark:text-white">Headers</h2>
    <div class="flex flex-col">
        @foreach ($headers as $key => $value)
        <div class="flex max-w-full items-baseline gap-2 h-10 text-sm font-mono">
            <div class="uppercase text-neutral-500 dark:text-neutral-400 shrink-0">{{ $key }}</div>
            <div class="min-w-6 grow h-3 border-b-2 border-dotted border-neutral-300 dark:border-white/20"></div>
            <div class="truncate text-neutral-900 dark:text-white">
            <span data-tippy-content="{{ $value }}">
                    {{ $value }}
                </span>
            </div>
        </div>
        @endforeach
    </div>
</div>


Review comment (suggestion):
```suggestion
                <span data-tippy-content="{{ $value }}">
```

Code after:
@props(['headers'])

<div class="flex flex-col gap-3">
    <h2 class="text-lg font-semibold text-neutral-900 dark:text-white">Headers</h2>
    <div class="flex flex-col">
        @foreach ($headers as $key => $value)
        <div class="flex max-w-full items-baseline gap-2 h-10 text-sm font-mono">
            <div class="uppercase text-neutral-500 dark:text-neutral-400 shrink-0">{{ $key }}</div>
            <div class="min-w-6 grow h-3 border-b-2 border-dotted border-neutral-300 dark:border-white/20"></div>
            <div class="truncate text-neutral-900 dark:text-white">
                <span data-tippy-content="{{ $value }}">
                    {{ $value }}
                </span>
            </div>
        </div>
        @endforeach
    </div>
</div>
Repository: laravel/framework
File: src/Illuminate/Cache/FailoverStore.php (PHP)

Code before:
    }

    /**
     * Get the cache key prefix.
     *
     * @return string
     */
    public function getPrefix()
    {
        return $this->attemptOnAllStores(__FUNCTION__, func_get_args());
    }

    /**
     * Attempt the given method on all stores.
     */
    protected function attemptOnAllStores(string $method, array $arguments)
    {
        $lastException = null;

        foreach ($this->stores as $store) {
            try {
                return $this->store($store)->{$method}(...$arguments);
            } catch (Throwable $e) {
                $lastException = $e;

                $this->events->dispatch(new CacheFailedOver($store));
            }
        }

        throw $lastException ?? new RuntimeException('All failover cache stores failed.');
    }

    /**
     * Get the cache store for the given store name.
     *
     * @return \Illuminate\Contracts\Cache\Store
     */
    protected function store(string $store)
    {
        return $this->cache->store($store)->getStore();
    }
}


Review comment (bug):


```suggestion
                $this->events->dispatch(new CacheFailedOver($store, $e));
```

Code after:
    }

    /**
     * Get the cache key prefix.
     *
     * @return string
     */
    public function getPrefix()
    {
        return $this->attemptOnAllStores(__FUNCTION__, func_get_args());
    }

    /**
     * Attempt the given method on all stores.
     */
    protected function attemptOnAllStores(string $method, array $arguments)
    {
        $lastException = null;

        foreach ($this->stores as $store) {
            try {
                return $this->store($store)->{$method}(...$arguments);
            } catch (Throwable $e) {
                $lastException = $e;

                $this->events->dispatch(new CacheFailedOver($store, $e));
            }
        }

        throw $lastException ?? new RuntimeException('All failover cache stores failed.');
    }

    /**
     * Get the cache store for the given store name.
     *
     * @return \Illuminate\Contracts\Cache\Store
     */
    protected function store(string $store)
    {
        return $this->cache->store($store)->getStore();
    }
}
Repository: laravel/framework
File: src/Illuminate/Cache/Events/CacheFailedOver.php (PHP)

Code before:
<?php

namespace Illuminate\Cache\Events;

class CacheFailedOver
{
    /**
     * Create a new event instance.
     *
     * @param  string  $storeName  The name of the cache store that failed.
     */
    public function __construct(
        public ?string $storeName,
    ) {
    }
}


Review comment (bug):
```suggestion
namespace Illuminate\Cache\Events;

use Throwable;

class CacheFailedOver
{
    /**
     * Create a new event instance.
     *
     * @param  string  $storeName  The name of the cache store that failed.
     */
    public function __construct(
        public ?string $storeName,
        public Throwable $exception,
    ) {
    }
}
```

Code after:
<?php

namespace Illuminate\Cache\Events;

use Throwable;

class CacheFailedOver
{
    /**
     * Create a new event instance.
     *
     * @param  string  $storeName  The name of the cache store that failed.
     */
    public function __construct(
        public ?string $storeName,
        public Throwable $exception,
    ) {
    }
}
Repository: laravel/framework
File: src/Illuminate/Cache/FileStore.php (PHP)

Code before:
    /**
     * Store an item in the cache indefinitely.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return bool
     */
    public function forever($key, $value)
    {
        return $this->put($key, $value, 0);
    }

    /**
     * Get a lock instance.
     *
     * @param  string  $name
     * @param  int  $seconds
     * @param  string|null  $owner
     * @return \Illuminate\Contracts\Cache\Lock
     */
    public function lock($name, $seconds = 0, $owner = null)
    {
        $this->ensureCacheDirectoryExists($this->lockDirectory ?? $this->directory);

        $lockName = "illuminate:cache:lock:{$name}";

        return new FileLock(
            new static($this->files, $this->lockDirectory ?? $this->directory, $this->filePermission),
            $lockName,
            $seconds,
            $owner
        );
    }

    /**
     * Restore a lock instance using the owner identifier.
     *
     * @param  string  $name
     * @param  string  $owner
     * @return \Illuminate\Contracts\Cache\Lock
     */
    public function restoreLock($name, $owner)
    {
        return $this->lock($name, 0, $owner);
    }

    /**
     * Remove an item from the cache.
     *
     * @param  string  $key
     * @return bool

Review comment (suggestion):
The `illuminate:` prefix is generally reserved for internal keys.

Because this is a user space key, it feels weird to me to make it look internal. Certain monitoring systems, such as Pulse, Nightwatch, and Telescope would then start ignoring this key.

Perhaps, we suffix the key like we do with `:timer` to keep it in a user-land space.


```suggestion
        $lockName = "{$name}:file-store-lock";

```

Code after:
    /**
     * Store an item in the cache indefinitely.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return bool
     */
    public function forever($key, $value)
    {
        return $this->put($key, $value, 0);
    }

    /**
     * Get a lock instance.
     *
     * @param  string  $name
     * @param  int  $seconds
     * @param  string|null  $owner
     * @return \Illuminate\Contracts\Cache\Lock
     */
    public function lock($name, $seconds = 0, $owner = null)
    {
        $this->ensureCacheDirectoryExists($this->lockDirectory ?? $this->directory);

        return new FileLock(
            new static($this->files, $this->lockDirectory ?? $this->directory, $this->filePermission),
            "file-store-lock:{$name}",
            $seconds,
            $owner
        );
    }

    /**
     * Restore a lock instance using the owner identifier.
     *
     * @param  string  $name
     * @param  string  $owner
     * @return \Illuminate\Contracts\Cache\Lock
     */
    public function restoreLock($name, $owner)
    {
        return $this->lock($name, 0, $owner);
    }

    /**
     * Remove an item from the cache.
     *
     * @param  string  $key
     * @return bool
     */
    public function forget($key)
Repository: laravel/framework
File: src/Illuminate/Filesystem/Filesystem.php (PHP)

Code before:
            Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(),
            false
        );
    }

    /**
     * Get all of the directories within a given directory.
     *
     * @param  string  $directory
     * @return array
     */
    public function directories($directory)
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Ensure a directory exists.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @return void
     */
    public function ensureDirectoryExists($path, $mode = 0755, $recursive = true)
    {
        if (! $this->isDirectory($path)) {
            $this->makeDirectory($path, $mode, $recursive);
        }
    }

    /**
     * Create a directory.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @param  bool  $force
     * @return bool
     */
    public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
    {
        if ($force) {
            return @mkdir($path, $mode, $recursive);
        }

Review comment (suggestion):
You can narrow this down:
```suggestion
     * @return string[]
```
or
```suggestion
     * @return list<string>
```
or
```suggestion
     * @return array<int, string>
```

Code after:
            Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(),
            false
        );
    }

    /**
     * Get all of the directories within a given directory.
     *
     * @param  string  $directory
     * @return array
     */
    public function directories($directory)
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Get all the directories within a given directory (recursive).
     *
     * @return array
     */
    public function allDirectories(string $directory): array
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Ensure a directory exists.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @return void
     */
    public function ensureDirectoryExists($path, $mode = 0755, $recursive = true)
    {
        if (! $this->isDirectory($path)) {
            $this->makeDirectory($path, $mode, $recursive);
        }
Repository: laravel/framework
File: src/Illuminate/Filesystem/Filesystem.php (PHP)

Code before:
     */
    public function directories($directory)
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Ensure a directory exists.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @return void
     */
    public function ensureDirectoryExists($path, $mode = 0755, $recursive = true)
    {
        if (! $this->isDirectory($path)) {
            $this->makeDirectory($path, $mode, $recursive);
        }
    }

    /**
     * Create a directory.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @param  bool  $force
     * @return bool
     */
    public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
    {
        if ($force) {
            return @mkdir($path, $mode, $recursive);
        }

        return mkdir($path, $mode, $recursive);
    }

    /**
     * Move a directory.
     *
     * @param  string  $from
     * @param  string  $to
     * @param  bool  $overwrite

Review comment (suggestion):
You can keep the `return`:
```suggestion
        return array_map(
            fn (SplFileInfo $dir) => $dir->getPathname(),
            Finder::create()->in($directory)->directories()->sortByName(),
        );
```

Code after:
     */
    public function directories($directory)
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Get all the directories within a given directory (recursive).
     *
     * @return array
     */
    public function allDirectories(string $directory): array
    {
        $directories = [];

        foreach (Finder::create()->in($directory)->directories()->sortByName() as $dir) {
            $directories[] = $dir->getPathname();
        }

        return $directories;
    }

    /**
     * Ensure a directory exists.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @return void
     */
    public function ensureDirectoryExists($path, $mode = 0755, $recursive = true)
    {
        if (! $this->isDirectory($path)) {
            $this->makeDirectory($path, $mode, $recursive);
        }
    }

    /**
     * Create a directory.
     *
     * @param  string  $path
     * @param  int  $mode
     * @param  bool  $recursive
     * @param  bool  $force
     * @return bool
Repository: laravel/framework
File: src/Illuminate/Foundation/Application.php (PHP)

Code before:
     */
    public function configurationIsCached()
    {
        return is_file($this->getCachedConfigPath());
    }

    /**
     * Get the path to the configuration cache file.
     *
     * @return string
     */
    public function getCachedConfigPath()
    {
        return $this->normalizeCachePath('APP_CONFIG_CACHE', 'cache/config.php');
    }

    /**
     * Determine if the application routes are cached.
     *
     * @return bool
     */
    public function routesAreCached()
    {
        return ($this->bound('routes.cached')
                && $this->make('routes.cached') === true)
            || $this['files']->exists($this->getCachedRoutesPath());
    }

    /**
     * Get the path to the routes cache file.
     *
     * @return string
     */
    public function getCachedRoutesPath()
    {
        return $this->normalizeCachePath('APP_ROUTES_CACHE', 'cache/routes-v7.php');
    }

    /**
     * Determine if the application events are cached.
     *
     * @return bool
     */
    public function eventsAreCached()
    {
        return $this['files']->exists($this->getCachedEventsPath());
    }

    /**
     * Get the path to the events cache file.
     *

Review comment (performance):
I think that this check can and should be cached (regardless if this PR is accepted)

Unless there's a reason to expect that during a run (either with the traditional PHP model or using Octane) this file would be deleted, we are repeatedly performing a file_exists check.

Code after:
     */
    public function configurationIsCached()
    {
        return is_file($this->getCachedConfigPath());
    }

    /**
     * Get the path to the configuration cache file.
     *
     * @return string
     */
    public function getCachedConfigPath()
    {
        return $this->normalizeCachePath('APP_CONFIG_CACHE', 'cache/config.php');
    }

    /**
     * Determine if the application routes are cached.
     *
     * @return bool
     */
    public function routesAreCached()
    {
        return ($this->bound('routes.cached') && $this->make('routes.cached') === true) ||
            $this['files']->exists($this->getCachedRoutesPath());
    }

    /**
     * Get the path to the routes cache file.
     *
     * @return string
     */
    public function getCachedRoutesPath()
    {
        return $this->normalizeCachePath('APP_ROUTES_CACHE', 'cache/routes-v7.php');
    }

    /**
     * Determine if the application events are cached.
     *
     * @return bool
     */
    public function eventsAreCached()
    {
        return $this['files']->exists($this->getCachedEventsPath());
    }

    /**
     * Get the path to the events cache file.
     *
     * @return string
Repository: laravel/framework
File: src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php (PHP)

Code before:

    /**
     * Merge the cast class and attribute cast attributes back into the model.
     *
     * @return void
     */
    protected function mergeAttributesFromCachedCasts()
    {
        $this->mergeAttributesFromClassCasts();
        $this->mergeAttributesFromAttributeCasts();
    }

    /**
     * Merge the a cast class and attribute cast attribute back into the model.
     *
     * @return void
     */
    protected function mergeAttributeFromCachedCasts(string $key)
    {
        $this->mergeAttributeFromClassCasts($key);
        $this->mergeAttributeFromAttributeCasts($key);
    }

    /**
     * Merge the cast class attributes back into the model.
     *
     * @return void
     */
    protected function mergeAttributesFromClassCasts()
    {
        foreach ($this->classCastCache as $key => $value) {
            $this->mergeAttributeFromClassCasts($key);
        }
    }

    private function mergeAttributeFromClassCasts(string $key): void
    {
        if (! isset($this->classCastCache[$key])) {
            return;
        }

        $value = $this->classCastCache[$key];

        $caster = $this->resolveCasterClass($key);

        $this->attributes = array_merge(
            $this->attributes,
            $caster instanceof CastsInboundAttributes
                ? [$key => $value]
                : $this->normalizeCastClassResponse($key, $caster->set($this, $key, $value, $this->attributes))
        );

Review comment (performance):
this is the same as mergeAttributesFromCachedCasts, but it uses the new extracted methods mergeAttributeFromClassCasts and mergeAttributeFromAttributeCasts that allow to do it for just one attribute

Code after:
     * @return string
     */
    protected function parseCasterClass($class)
    {
        return ! str_contains($class, ':')
            ? $class
            : explode(':', $class, 2)[0];
    }

    /**
     * Merge the cast class and attribute cast attributes back into the model.
     *
     * @return void
     */
    protected function mergeAttributesFromCachedCasts()
    {
        $this->mergeAttributesFromClassCasts();
        $this->mergeAttributesFromAttributeCasts();
    }

    /**
     * Merge the a cast class and attribute cast attribute back into the model.
     *
     * @return void
     */
    protected function mergeAttributeFromCachedCasts(string $key)
    {
        $this->mergeAttributeFromClassCasts($key);
        $this->mergeAttributeFromAttributeCasts($key);
    }

    /**
     * Merge the cast class attributes back into the model.
     *
     * @return void
     */
    protected function mergeAttributesFromClassCasts()
    {
        foreach ($this->classCastCache as $key => $value) {
            $this->mergeAttributeFromClassCasts($key);
        }
    }

    /**
     * Merge the cast class attribute back into the model.
     *
     * @return void
     */
    protected function mergeAttributeFromClassCasts(string $key): void
    {
        if (! isset($this->classCastCache[$key])) {