EscolaLMS/model-fields

View on GitHub
src/Services/ModelFieldsService.php

Summary

Maintainability
A
1 hr
Test Coverage
A
93%

Method getExtraAttributesValues has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function getExtraAttributesValues(Model $model, ?int $visibility = null): array
    {
        if (config('model-fields.enabled')) {
            if (!array_key_exists('id', $model->getAttributes())) {
                return [];
Severity: Minor
Found in src/Services/ModelFieldsService.php - About 1 hr to fix

    Method addOrUpdateMetadataField has 7 arguments (exceeds 4 allowed). Consider refactoring.
    Open

        public function addOrUpdateMetadataField(string $class_type, string $name, string $type, string $default = '', array $rules = null, int $visibility = 1 << 0, array $extra = null): Metadata
    Severity: Major
    Found in src/Services/ModelFieldsService.php - About 50 mins to fix

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'addOrUpdateMetadataField'.
      Open

              Cache::flush();
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\EscolaLms\ModelFields\Enum\MetaFieldTypeEnum' in method 'addOrUpdateMetadataField'.
      Open

              if (!MetaFieldTypeEnum::hasValue($type)) {
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\EscolaLms\ModelFields\Models\Metadata' in method 'addOrUpdateMetadataField'.
      Open

              return Metadata::updateOrCreate(
                  ['class_type' => $class_type, 'name' => $name],
                  ['type' => $type, 'default' => $default, 'rules' => $rules, 'visibility' => $visibility, 'extra' => $extra]
              );
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\EscolaLms\ModelFields\Enum\MetaFieldTypeEnum' in method 'addOrUpdateMetadataField'.
      Open

                      'type' => [sprintf('type must be one of %s', implode(",", MetaFieldTypeEnum::getValues()))],
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Validation\ValidationException' in method 'addOrUpdateMetadataField'.
      Open

                  throw ValidationException::withMessages([
                      'type' => [sprintf('type must be one of %s', implode(",", MetaFieldTypeEnum::getValues()))],
                  ]);
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'removeMetaField'.
      Open

              Cache::flush();
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'getFieldsMetadata'.
      Open

                  Cache::put('model_fields_metadata_table_exists', $tableExist);
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'getFieldsMetadata'.
      Open

                  return Cache::rememberForever($key, function () use ($class_type) {
                      try {
                          $classTypes = class_parents($class_type) ? array_merge([$class_type], class_parents($class_type)) : [$class_type];
                          return Metadata::whereIn('class_type', $classTypes)->get();
                      } catch (QueryException $qe) {
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'getFieldsMetadata'.
      Open

                          Cache::put('model_fields_metadata_table_exists', false);
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Schema' in method 'getFieldsMetadata'.
      Open

                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Schema' in method 'getFieldsMetadata'.
      Open

                  $tableExist = Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'getFieldsMetadata'.
      Open

              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
              });
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'getExtraAttributesValues'.
      Open

                  $modelFields = Cache::rememberForever($key, function () use ($model) {
                      // @phpstan-ignore-next-line
                      return $model->fields()->get();
                  });
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      StaticAccess

      Since: 1.4.0

      Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

      Example

      class Foo
      {
          public function bar()
          {
              Bar::baz();
          }
      }

      Source https://phpmd.org/rules/cleancode.html#staticaccess

      Avoid unused parameters such as '$key'.
      Open

                      ->mapWithKeys(fn($item, $key) => [$item['name'] => is_array($item['rules']) ? $item['rules'] : []])
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      UnusedFormalParameter

      Since: 0.2

      Avoid passing parameters to methods or constructors and then not using those parameters.

      Example

      class Foo
      {
          private function bar($howdy)
          {
              // $howdy is not used
          }
      }

      Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

      syntax error, unexpected '|', expecting ';' or '{'
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
      Severity: Critical
      Found in src/Services/ModelFieldsService.php by phan

      Avoid excessively long variable names like $metadataFieldVisibility. Keep variable name length under 20.
      Open

          private function checkVisibility(?int $visibility = null, int $metadataFieldVisibility): int|bool
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      LongVariable

      Since: 0.2

      Detects when a field, formal or local variable is declared with a long name.

      Example

      class Something {
          protected $reallyLongIntName = -3; // VIOLATION - Field
          public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
              $otherReallyLongName = -5; // VIOLATION - Local
              for ($interestingIntIndex = 0; // VIOLATION - For
                   $interestingIntIndex < 10;
                   $interestingIntIndex++ ) {
              }
          }
      }

      Source https://phpmd.org/rules/naming.html#longvariable

      The parameter $class_type is not named in camelCase.
      Open

          public function removeMetaField(string $class_type, string $name): bool
          {
              $bool = Metadata::where(
                  ['class_type' => $class_type, 'name' => $name]
              )->delete();
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseParameterName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name parameters.

      Example

      class ClassName {
          public function doSomething($user_name) {
          }
      }

      Source

      The parameter $class_type is not named in camelCase.
      Open

          public function addOrUpdateMetadataField(string $class_type, string $name, string $type, string $default = '', array $rules = null, int $visibility = 1 << 0, array $extra = null): Metadata
          {
              if (!MetaFieldTypeEnum::hasValue($type)) {
                  throw ValidationException::withMessages([
                      'type' => [sprintf('type must be one of %s', implode(",", MetaFieldTypeEnum::getValues()))],
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseParameterName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name parameters.

      Example

      class ClassName {
          public function doSomething($user_name) {
          }
      }

      Source

      The parameter $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseParameterName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name parameters.

      Example

      class ClassName {
          public function doSomething($user_name) {
          }
      }

      Source

      The parameter $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
          {
              if (!config('model-fields.enabled')) {
                  return collect([]);
              }
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseParameterName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name parameters.

      Example

      class ClassName {
          public function doSomething($user_name) {
          }
      }

      Source

      The parameter $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataRules(string $class_type): array
          {
              if (config('model-fields.enabled')) {
                  return $this->getFieldsMetadata($class_type)
                      ->mapWithKeys(fn($item, $key) => [$item['name'] => is_array($item['rules']) ? $item['rules'] : []])
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseParameterName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name parameters.

      Example

      class ClassName {
          public function doSomething($user_name) {
          }
      }

      Source

      Line exceeds 120 characters; contains 151 characters
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator

      Line exceeds 120 characters; contains 192 characters
      Open

          public function addOrUpdateMetadataField(string $class_type, string $name, string $type, string $default = '', array $rules = null, int $visibility = 1 << 0, array $extra = null): Metadata

      Line exceeds 120 characters; contains 122 characters
      Open

              $classTypes = class_parents($class_type) ? array_merge([$class_type], class_parents($class_type)) : [$class_type];

      Line exceeds 120 characters; contains 134 characters
      Open

                      ->mapWithKeys(fn($item, $key) => [$item['name'] => self::castField($item['value'], ($fields[$item['name']] ?? null))])

      Line exceeds 120 characters; contains 134 characters
      Open

                          $classTypes = class_parents($class_type) ? array_merge([$class_type], class_parents($class_type)) : [$class_type];

      The variable $class_type is not named in camelCase.
      Open

          public function addOrUpdateMetadataField(string $class_type, string $name, string $type, string $default = '', array $rules = null, int $visibility = 1 << 0, array $extra = null): Metadata
          {
              if (!MetaFieldTypeEnum::hasValue($type)) {
                  throw ValidationException::withMessages([
                      'type' => [sprintf('type must be one of %s', implode(",", MetaFieldTypeEnum::getValues()))],
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
          {
              if (!config('model-fields.enabled')) {
                  return collect([]);
              }
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
          {
              if (!config('model-fields.enabled')) {
                  return collect([]);
              }
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function removeMetaField(string $class_type, string $name): bool
          {
              $bool = Metadata::where(
                  ['class_type' => $class_type, 'name' => $name]
              )->delete();
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function removeMetaField(string $class_type, string $name): bool
          {
              $bool = Metadata::where(
                  ['class_type' => $class_type, 'name' => $name]
              )->delete();
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataRules(string $class_type): array
          {
              if (config('model-fields.enabled')) {
                  return $this->getFieldsMetadata($class_type)
                      ->mapWithKeys(fn($item, $key) => [$item['name'] => is_array($item['rules']) ? $item['rules'] : []])
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
          {
              if (!config('model-fields.enabled')) {
                  return collect([]);
              }
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadataListPaginated(string $class_type, ?int $perPage = 15, ?OrderDto $orderDto = null): Collection|LengthAwarePaginator
          {
              if (!config('model-fields.enabled')) {
                  return collect([]);
              }
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      The variable $class_type is not named in camelCase.
      Open

          public function getFieldsMetadata(string $class_type): Collection
          {
              // add result of hasTable to the cache to limit database queries
              $tableExist = Cache::rememberForever('model_fields_metadata_table_exists', function () {
                  return Schema::hasTable('model_fields_metadata');
      Severity: Minor
      Found in src/Services/ModelFieldsService.php by phpmd

      CamelCaseVariableName

      Since: 0.2

      It is considered best practice to use the camelCase notation to name variables.

      Example

      class ClassName {
          public function doSomething() {
              $data_module = new DataModule();
          }
      }

      Source

      There are no issues that match your filters.

      Category
      Status