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

Code before:
     */
    public $relations;

    /**
     * The connection name of the model.
     *
     * @var string|null
     */
    public $connection;

    /**
     * The class name of the model collection.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Collection>|null
     */
    public $collectionClass;

    /**
     * Create a new model identifier.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Model>|null  $class
     * @param  mixed  $id
     * @param  array  $relations
     * @param  mixed  $connection
     */
    public function __construct($class, $id, array $relations, $connection)
    {
        $this->class = $class === null ? null : Relation::getMorphAlias($class);
        $this->id = $id;
        $this->relations = $relations;
        $this->connection = $connection;
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;

        return $this;
    }

    /**
     * @return class-string<\Illuminate\Database\Eloquent\Model>|null
     */
    public function getClass(): ?string
    {

Review comment (suggestion):
This is actually nullable because if a queueable collection is empty, it just maps to null.

Code after:

    /**
     * The relationships loaded on the model.
     *
     * @var array
     */
    public $relations;

    /**
     * The connection name of the model.
     *
     * @var string|null
     */
    public $connection;

    /**
     * The class name of the model collection.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Collection>|null
     */
    public $collectionClass;

    /**
     * Create a new model identifier.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Model>|null  $class
     * @param  mixed  $id
     * @param  array  $relations
     * @param  mixed  $connection
     */
    public function __construct($class, $id, array $relations, $connection)
    {
        if ($class !== null && self::$useMorphMap) {
            $class = Relation::getMorphAlias($class);
        }

        $this->class = $class;
        $this->id = $id;
        $this->relations = $relations;
        $this->connection = $connection;
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;
Repository: laravel/framework
File: src/Illuminate/Contracts/Database/ModelIdentifier.php (PHP)

Code before:

    /**
     * The connection name of the model.
     *
     * @var string|null
     */
    public $connection;

    /**
     * The class name of the model collection.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Collection>|null
     */
    public $collectionClass;

    /**
     * Create a new model identifier.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Model>|null  $class
     * @param  mixed  $id
     * @param  array  $relations
     * @param  mixed  $connection
     */
    public function __construct($class, $id, array $relations, $connection)
    {
        $this->class = $class === null ? null : Relation::getMorphAlias($class);
        $this->id = $id;
        $this->relations = $relations;
        $this->connection = $connection;
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;

        return $this;
    }

    /**
     * @return class-string<\Illuminate\Database\Eloquent\Model>|null
     */
    public function getClass(): ?string
    {
        if ($this->class === null) {
            return null;

Review comment (suggestion):
Maybe do this to opt-in on the class-level?
```php
$this->class = $class === null ? null : (
    in_array(SerializesWithMorphMap::class, class_implements($class) ?: [])
    ? Relation::getMorphAlias($class)
    : $class
);
```

Code after:
     * The relationships loaded on the model.
     *
     * @var array
     */
    public $relations;

    /**
     * The connection name of the model.
     *
     * @var string|null
     */
    public $connection;

    /**
     * The class name of the model collection.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Collection>|null
     */
    public $collectionClass;

    /**
     * Create a new model identifier.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Model>|null  $class
     * @param  mixed  $id
     * @param  array  $relations
     * @param  mixed  $connection
     */
    public function __construct($class, $id, array $relations, $connection)
    {
        if ($class !== null && self::$useMorphMap) {
            $class = Relation::getMorphAlias($class);
        }

        $this->class = $class;
        $this->id = $id;
        $this->relations = $relations;
        $this->connection = $connection;
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;

        return $this;
Repository: laravel/framework
File: src/Illuminate/Contracts/Database/ModelIdentifier.php (PHP)

Code before:
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;

        return $this;
    }

    /**
     * @return class-string<\Illuminate\Database\Eloquent\Model>|null
     */
    public function getClass(): ?string
    {
        if ($this->class === null) {
            return null;
        }

        return self::$useMorphMap
            ? (Relation::getMorphedModel($this->class) ?? $this->class)
            : $this->class;
    }

    /**
     * Indicate whether to use the relational morph-map when serializing Models.
     */
    public static function useMorphMap(bool $useMorphMap = true): void
    {
        static::$useMorphMap = $useMorphMap;
    }

    /**
     * Flush the ModelIdentifier's shared state.
     */
    public static function flushState(): void
    {
        self::$useMorphMap = false;
    }
}


Review comment (suggestion):
I think this should go back to simply `return Relation::getMorphedModel($this->class) ?? $this->class`. Because if somebody ever opts out at any point, running queues would break.

Code after:
    }

    /**
     * Specify the collection class that should be used when serializing / restoring collections.
     *
     * @param  class-string<\Illuminate\Database\Eloquent\Collection>  $collectionClass
     * @return $this
     */
    public function useCollectionClass(?string $collectionClass)
    {
        $this->collectionClass = $collectionClass;

        return $this;
    }

    /**
     * Get the fully-qualified class name of the Model.
     *
     * @return class-string<\Illuminate\Database\Eloquent\Model>|null
     */
    public function getClass(): ?string
    {
        if ($this->class === null) {
            return null;
        }

        return Relation::getMorphedModel($this->class) ?? $this->class;
    }

    /**
     * Indicate whether to use the relational morph-map when serializing Models.
     */
    public static function useMorphMap(bool $useMorphMap = true): void
    {
        static::$useMorphMap = $useMorphMap;
    }
}
Repository: laravel/framework
File: src/Illuminate/Collections/helpers.php (PHP)

Code before:
     * Return the default value of the given value.
     *
     * @template TValue
     * @template TArgs
     *
     * @param  TValue|\Closure(TArgs): TValue  $value
     * @param  TArgs  ...$args
     * @return TValue
     */
    function value($value, ...$args)
    {
        return $value instanceof Closure ? $value(...$args) : $value;
    }
}

if (! function_exists('when')) {
    /**
     * Return a value if the given condition is true.
     *
     * @template TValue
     * @template TDefault
     *
     * @param  mixed  $condition
     * @param  TValue|\Closure(): TValue  $value
     * @param  TDefault|\Closure(): TDefault  $default
     * @return ($condition is true ? TValue : ($condition is positive-int ? TValue : ($condition is non-falsy-string ? TValue : ($condition is non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault)))))
     */
    function when($condition, $value, $default = null)
    {
        $condition = $condition instanceof Closure ? $condition() : $condition;

        if ($condition) {
            return value($value, $condition);
        }

        return value($default, $condition);
    }
}


Review comment (suggestion):
You can work with a union type here if you want:
```suggestion
     * @return ($condition is true|positive-int|non-falsy-string|non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault))
```

Code after:
     * Return the default value of the given value.
     *
     * @template TValue
     * @template TArgs
     *
     * @param  TValue|\Closure(TArgs): TValue  $value
     * @param  TArgs  ...$args
     * @return TValue
     */
    function value($value, ...$args)
    {
        return $value instanceof Closure ? $value(...$args) : $value;
    }
}

if (! function_exists('when')) {
    /**
     * Return a value if the given condition is true.
     *
     * @template TValue
     * @template TDefault
     *
     * @param  mixed  $condition
     * @param  TValue|\Closure(): TValue  $value
     * @param  TDefault|\Closure(): TDefault  $default
     * @return ($condition is true|positive-int|non-falsy-string|non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault))
     */
    function when($condition, $value, $default = null)
    {
        $condition = $condition instanceof Closure ? $condition() : $condition;

        if ($condition) {
            return value($value, $condition);
        }

        return value($default, $condition);
    }
}
Repository: laravel/framework
File: src/Illuminate/Process/PendingProcess.php (PHP)

Code before:
        return $this;
    }

    /**
     * Specify the maximum number of seconds the process may run.
     *
     * @param  int  $timeout
     * @return $this
     */
    public function timeout(int $timeout)
    {
        $this->timeout = $timeout;

        return $this;
    }

    /**
     * Specify the maximum number of seconds a process may go without returning output.
     *
     * @param  int  $timeout
     * @return $this
     */
    public function idleTimeout(int $timeout)
    {
        $this->idleTimeout = $timeout;

        return $this;
    }

    /**
     * Indicate that the process may run forever without timing out.
     *
     * @return $this
     */
    public function forever()
    {
        $this->timeout = null;

        return $this;
    }

    /**
     * Set the additional environment variables for the process.
     *
     * @param  array  $environment
     * @return $this
     */
    public function env(array $environment)
    {
        $this->environment = $environment;


Review comment (refactor):
Maybe, we should have a similar helper as `enum_value()` and use this across the whole codebase 🤔 

Code after:

        return $this;
    }

    /**
     * Specify the maximum number of seconds the process may run.
     *
     * @param  CarbonInterval|int  $timeout
     * @return $this
     */
    public function timeout(CarbonInterval|int $timeout)
    {
        $this->timeout = $timeout instanceof CarbonInterval ? (int) $timeout->totalSeconds : $timeout;

        return $this;
    }

    /**
     * Specify the maximum number of seconds a process may go without returning output.
     *
     * @param  CarbonInterval|int  $timeout
     * @return $this
     */
    public function idleTimeout(CarbonInterval|int $timeout)
    {
        $this->idleTimeout = $timeout instanceof CarbonInterval ? (int) $timeout->totalSeconds : $timeout;

        return $this;
    }

    /**
     * Indicate that the process may run forever without timing out.
     *
     * @return $this
     */
    public function forever()
    {
        $this->timeout = null;

        return $this;
    }

    /**
     * Set the additional environment variables for the process.
     *
     * @param  array  $environment
     * @return $this
     */
    public function env(array $environment)
    {
        $this->environment = $environment;
Repository: leon-ai/leon
File: server/src/stt/coqui-stt/parser.js (JavaScript)

Code before:
import wav from 'node-wav'
import fs from 'fs'

import log from '@/helpers/log'

log.title('Coqui-ai Parser')

const parser = { }
let STT = { }

/* istanbul ignore next */
try {
  STT = require('stt-gpu') // eslint-disable-line global-require, import/no-unresolved

  log.success('GPU version found')
} catch (eGpu) {
  log.info('GPU version not found, trying to get the CPU version...')

  try {
    STT = require('stt') // eslint-disable-line global-require, import/no-unresolved

    log.success('CPU version found')
  } catch (eCpu) {
    log.error(`No Coqui-ai library found:\nGPU: ${eGpu}\nCPU: ${eCpu}`)
  }
}

let model = { }
let desiredSampleRate = 16000

/**
 * Model and language model paths
 */
parser.conf = {
  model: 'bin/coqui/model.tflite',
  scorer: 'bin/coqui/huge-vocabulary.scorer'
}


Review comment (suggestion):
Coqui only releases TFLite models, so there's no GPU needed. `stt-gpu` is depreciated. You can read more detail [here](https://github.com/coqui-ai/STT/discussions/1922).

Code after:
import wav from 'node-wav'
import fs from 'fs'
import { Model } from 'stt'

import log from '@/helpers/log'

log.title('Coqui-ai Parser')

const parser = { }
let model = { }
let desiredSampleRate = 16000

/**
 * Model and language model paths
 */
parser.conf = {
  model: 'bin/coqui/model.tflite',
  scorer: 'bin/coqui/huge-vocabulary.scorer'
}

/**
 * Load models
 */
parser.init = (args) => {
  log.info(`Loading model from file ${args.model}...`)

  if (!fs.existsSync(args.model)) {
    log.error(`Cannot find ${args.model}. You can setup the offline STT by running: "npm run setup:offline-stt"`)

    return false
  }

  if (!fs.existsSync(args.scorer)) {
    log.error(`Cannot find ${args.scorer}. You can setup the offline STT by running: "npm run setup:offline-stt"`)

    return false
  }
Repository: localsend/localsend
File: lib/util/native/get_destination_directory.dart (Dart)

Code before:
import 'dart:io' show Directory, Platform;

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart' as path;
import 'package:shared_storage/shared_storage.dart' as shared_storage;

Future<String> getDefaultDestinationDirectory() async {
  switch (defaultTargetPlatform) {
    case TargetPlatform.android:
      // ignore: deprecated_member_use
      final dir = await shared_storage.getExternalStoragePublicDirectory(shared_storage.EnvironmentDirectory.downloads);
      return dir?.path ?? '/storage/emulated/0/Download';
    case TargetPlatform.iOS:
      return (await path.getApplicationDocumentsDirectory()).path;
    case TargetPlatform.linux:
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
    case TargetPlatform.fuchsia:
      var downloadDir = await path.getDownloadsDirectory();
      if (defaultTargetPlatform == TargetPlatform.windows) {
        downloadDir = Directory('${Platform.environment['HOMEPATH']}/Downloads');
      } else {
        downloadDir = Directory('${Platform.environment['HOME']}/Downloads');
      }
      return downloadDir.path.replaceAll('\\', '/');
  }
}


Review comment (suggestion):
Thank you for this fix. I think there is a missing null check if `path.getDownloadsDirectory()` returns not null.

Also please check if the new path `~/Downloads` exists with `Directory(path).existsSync()` and fallback to `~`.

Code after:
import 'dart:io' show Directory, Platform;

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart' as path;
import 'package:shared_storage/shared_storage.dart' as shared_storage;

Future<String> getDefaultDestinationDirectory() async {
  switch (defaultTargetPlatform) {
    case TargetPlatform.android:
      // ignore: deprecated_member_use
      final dir = await shared_storage.getExternalStoragePublicDirectory(shared_storage.EnvironmentDirectory.downloads);
      return dir?.path ?? '/storage/emulated/0/Download';
    case TargetPlatform.iOS:
      return (await path.getApplicationDocumentsDirectory()).path;
    case TargetPlatform.linux:
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
    case TargetPlatform.fuchsia:
      var downloadDir = await path.getDownloadsDirectory();
      if (downloadDir == null) {
        if (defaultTargetPlatform == TargetPlatform.windows) {
          downloadDir = Directory('${Platform.environment['HOMEPATH']}/Downloads');
          if (!downloadDir.existsSync()) {
            downloadDir = Directory(Platform.environment['HOMEPATH']!);
          }
        } else {
          downloadDir = Directory('${Platform.environment['HOME']}/Downloads');
          if (!downloadDir.existsSync()) {
            downloadDir = Directory(Platform.environment['HOME']!);
          }
        }
      }
      return downloadDir.path.replaceAll('\\', '/');
  }
}
Repository: localsend/localsend
File: app/lib/provider/persistence_provider.dart (Dart)

Code before:
  AppLocale? getLocale() {
    final value = _prefs.getString(_localeKey);
    if (value == null) {
      return null;
    }
    return AppLocale.values.firstWhereOrNull((locale) => locale.languageTag == value);
  }

  Future<void> setLocale(AppLocale? locale) async {
    if (locale == null) {
      await _prefs.remove(_localeKey);
    } else {
      await _prefs.setString(_localeKey, locale.languageTag);
    }
  }

  int getPort() {
    return _prefs.getInt(_portKey) ?? defaultPort;
  }

  Future<void> setPort(int port) async {
    await _prefs.setInt(_portKey, port);
  }

  int getAnonymousAccess() {
    return _prefs.getInt(_anonymousAccess) ?? 0;
  }

  Future<void> setAnonymousAccess(int anonymousAccess) async {
    await _prefs.setInt(_anonymousAccess, anonymousAccess);
  }

  String getMulticastGroup() {
    return _prefs.getString(_multicastGroupKey) ?? defaultMulticastGroup;
  }

  Future<void> setMulticastGroup(String group) async {
    await _prefs.setString(_multicastGroupKey, group);
  }

  String? getDestination() {
    return _prefs.getString(_destinationKey);
  }

  Future<void> setDestination(String? destination) async {
    if (destination == null) {
      await _prefs.remove(_destinationKey);
    } else {
      await _prefs.setString(_destinationKey, destination);
    }
  }

Review comment (bug):
If you intend to have 3 or more options, then an enum should be used (see `getSendMode`, `setSendMode` below).
An `int` is not type safe enough which could lead to bugs.

Code after:
    if (value == null) {
      return null;
    }
    return AppLocale.values.firstWhereOrNull((locale) => locale.languageTag == value);
  }

  Future<void> setLocale(AppLocale? locale) async {
    if (locale == null) {
      await _prefs.remove(_localeKey);
    } else {
      await _prefs.setString(_localeKey, locale.languageTag);
    }
  }

  int getPort() {
    return _prefs.getInt(_portKey) ?? defaultPort;
  }

  Future<void> setPort(int port) async {
    await _prefs.setInt(_portKey, port);
  }

  bool getEnableGlobalAnonymousAccess() {
    return _prefs.getBool(_anonymousAccessForGlobal) ?? false;
  }

  Future<void> setEnableGlobalAnonymousAccess(bool anonymousAccess) async {
    await _prefs.setBool(_anonymousAccessForGlobal, anonymousAccess);
  }

  bool getEnableTemporaryAnonymousAccess() {
    return _prefs.getBool(_anonymousAccessForTemporary) ?? false;
  }

  Future<void> setEnableTemporaryAnonymousAccess(bool v) async {
    await _prefs.setBool(_anonymousAccessForTemporary, v);
  }

  String getMulticastGroup() {
    return _prefs.getString(_multicastGroupKey) ?? defaultMulticastGroup;
  }

  Future<void> setMulticastGroup(String group) async {
    await _prefs.setString(_multicastGroupKey, group);
  }

  String? getDestination() {
    return _prefs.getString(_destinationKey);
  }

  Future<void> setDestination(String? destination) async {
Repository: localsend/localsend
File: app/lib/provider/persistence_provider.dart (Dart)

Code before:
      return null;
    }
    return AppLocale.values.firstWhereOrNull((locale) => locale.languageTag == value);
  }

  Future<void> setLocale(AppLocale? locale) async {
    if (locale == null) {
      await _prefs.remove(_localeKey);
    } else {
      await _prefs.setString(_localeKey, locale.languageTag);
    }
  }

  int getPort() {
    return _prefs.getInt(_portKey) ?? defaultDiscoveryTimeout;
  }

  int getDiscoveryTimeout() {
    return _prefs.getInt(_timeoutKey) ?? defaultDiscoveryTimeout;
  }

  Future<void> setPort(int port) async {
    await _prefs.setInt(_portKey, port);
  }

  Future<void> setDiscoveryTimeout(int timeout) async {
    await _prefs.setInt(_timeoutKey, timeout);
  }

  bool getShareViaLinkAutoAccept() {
    return _prefs.getBool(_shareViaLinkAutoAccept) ?? false;
  }

  Future<void> setShareViaLinkAutoAccept(bool shareViaLinkAutoAccept) async {
    await _prefs.setBool(_shareViaLinkAutoAccept, shareViaLinkAutoAccept);
  }

  String getMulticastGroup() {
    return _prefs.getString(_multicastGroupKey) ?? defaultMulticastGroup;
  }

  Future<void> setMulticastGroup(String group) async {
    await _prefs.setString(_multicastGroupKey, group);
  }

  String? getDestination() {
    return _prefs.getString(_destinationKey);
  }

  Future<void> setDestination(String? destination) async {
    if (destination == null) {

Review comment (suggestion):
Please order the getters and setters next to each other (`setDiscoveryTimeout` should be right below `getDiscoveryTimeout`)

Code after:
      return null;
    }
    return AppLocale.values.firstWhereOrNull((locale) => locale.languageTag == value);
  }

  Future<void> setLocale(AppLocale? locale) async {
    if (locale == null) {
      await _prefs.remove(_localeKey);
    } else {
      await _prefs.setString(_localeKey, locale.languageTag);
    }
  }

  int getPort() {
    return _prefs.getInt(_portKey) ?? defaultPort;
  }

  Future<void> setPort(int port) async {
    await _prefs.setInt(_portKey, port);
  }

  int getDiscoveryTimeout() {
    return _prefs.getInt(_timeoutKey) ?? defaultDiscoveryTimeout;
  }

  Future<void> setDiscoveryTimeout(int timeout) async {
    await _prefs.setInt(_timeoutKey, timeout);
  }

  bool getShareViaLinkAutoAccept() {
    return _prefs.getBool(_shareViaLinkAutoAccept) ?? false;
  }

  Future<void> setShareViaLinkAutoAccept(bool shareViaLinkAutoAccept) async {
    await _prefs.setBool(_shareViaLinkAutoAccept, shareViaLinkAutoAccept);
  }

  String getMulticastGroup() {
    return _prefs.getString(_multicastGroupKey) ?? defaultMulticastGroup;
  }

  Future<void> setMulticastGroup(String group) async {
    await _prefs.setString(_multicastGroupKey, group);
  }

  String? getDestination() {
    return _prefs.getString(_destinationKey);
  }

  Future<void> setDestination(String? destination) async {
    if (destination == null) {