Skip to content

Commit 6ed3278

Browse files
Fix casting problems and php-cs-fixer upgrade
1 parent d6fc0ba commit 6ed3278

12 files changed

+103
-52
lines changed

.github/workflows/php-cs-fixer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Fix style
1414
uses: docker://oskarstark/php-cs-fixer-ga
1515
with:
16-
args: --config=.php_cs --allow-risky=yes
16+
args: --config=.php_cs.dist.php --allow-risky=yes
1717

1818
- name: Extract branch name
1919
shell: bash

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ coverage
66
.phpunit.result.cache
77
.idea
88
.php_cs.cache
9-
9+
.php-cs-fixer.cache

.php_cs .php_cs.dist.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php
22

33
$finder = Symfony\Component\Finder\Finder::create()
4-
->notPath('bootstrap/*')
5-
->notPath('storage/*')
6-
->notPath('storage/*')
7-
->notPath('resources/view/mail/*')
84
->in([
95
__DIR__ . '/src',
106
__DIR__ . '/tests',
@@ -14,14 +10,14 @@
1410
->ignoreDotFiles(true)
1511
->ignoreVCS(true);
1612

17-
return PhpCsFixer\Config::create()
13+
return (new PhpCsFixer\Config())
1814
->setRules([
1915
'@PSR2' => true,
2016
'array_syntax' => ['syntax' => 'short'],
21-
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
17+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
2218
'no_unused_imports' => true,
2319
'not_operator_with_successor_space' => true,
24-
'trailing_comma_in_multiline_array' => true,
20+
'trailing_comma_in_multiline' => true,
2521
'phpdoc_scalar' => true,
2622
'unary_operator_spaces' => true,
2723
'binary_operator_spaces' => true,
@@ -30,9 +26,15 @@
3026
],
3127
'phpdoc_single_line_var_spacing' => true,
3228
'phpdoc_var_without_name' => true,
29+
'class_attributes_separation' => [
30+
'elements' => [
31+
'method' => 'one',
32+
],
33+
],
3334
'method_argument_space' => [
3435
'on_multiline' => 'ensure_fully_multiline',
3536
'keep_multiple_spaces_after_comma' => true,
36-
]
37+
],
38+
'single_trait_insert_per_statement' => true,
3739
])
3840
->setFinder($finder);

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `laravel-settings` will be documented in this file
44

5+
## 2.1.5 - 2021-05-21
6+
7+
- fix some casting problems
8+
- update php-cs-fixer
9+
510
## 2.1.4 - 2021-04-28
611

712
- added fallback for settings.auto_discover_settings (#63)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ class DateSettings extends Settings
560560
/** @var \DateTime */
561561
public $birth_date;
562562

563-
/** @var ?null */
563+
/** @var ?int */
564564
public $a_nullable_int;
565565

566566
/** @var int|null */

src/Factories/SettingsCastFactory.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
use Illuminate\Support\Str;
66
use phpDocumentor\Reflection\Type;
77
use phpDocumentor\Reflection\Types\AbstractList;
8+
use phpDocumentor\Reflection\Types\Boolean;
9+
use phpDocumentor\Reflection\Types\Float_;
10+
use phpDocumentor\Reflection\Types\Integer;
811
use phpDocumentor\Reflection\Types\Nullable;
912
use phpDocumentor\Reflection\Types\Object_;
13+
use phpDocumentor\Reflection\Types\String_;
1014
use ReflectionProperty;
1115
use Spatie\LaravelSettings\SettingsCasts\ArraySettingsCast;
1216
use Spatie\LaravelSettings\SettingsCasts\SettingsCast;
@@ -65,6 +69,14 @@ protected static function createLocalCast(
6569
protected static function createDefaultCast(
6670
Type $type
6771
): ?SettingsCast {
72+
$noCastRequired = self::isTypeWithNoCastRequired($type)
73+
|| ($type instanceof AbstractList && self::isTypeWithNoCastRequired($type->getValueType()))
74+
|| ($type instanceof Nullable && self::isTypeWithNoCastRequired($type->getActualType()));
75+
76+
if ($noCastRequired) {
77+
return null;
78+
}
79+
6880
if ($type instanceof AbstractList) {
6981
return new ArraySettingsCast(self::createDefaultCast($type->getValueType()));
7082
}
@@ -88,6 +100,14 @@ protected static function createDefaultCast(
88100
return null;
89101
}
90102

103+
protected static function isTypeWithNoCastRequired(Type $type): bool
104+
{
105+
return $type instanceof Integer
106+
|| $type instanceof Boolean
107+
|| $type instanceof Float_
108+
|| $type instanceof String_;
109+
}
110+
91111
protected static function shouldCast(string $type, string $base): bool
92112
{
93113
return $type === $base

src/SettingsRepositories/DatabaseSettingsRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getPropertiesInGroup(string $group): array
2525
* @var \Spatie\LaravelSettings\Models\SettingsProperty $temp
2626
* @psalm-suppress UndefinedClass
2727
*/
28-
$temp = new $this->propertyModel;
28+
$temp = new $this->propertyModel();
2929

3030
return DB::connection($this->connection ?? $temp->getConnectionName())
3131
->table($temp->getTable())

tests/Factories/SettingsCastFactoryTest.php

+35-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SettingsCastFactoryTest extends TestCase
1616
/** @test */
1717
public function it_will_not_resolve_a_cast_for_built_in_types()
1818
{
19-
$fake = new class {
19+
$fake = new class() {
2020
public int $integer;
2121
};
2222

@@ -30,7 +30,7 @@ public function it_will_not_resolve_a_cast_for_built_in_types()
3030
/** @test */
3131
public function it_can_resolve_a_global_cast()
3232
{
33-
$fake = new class {
33+
$fake = new class() {
3434
public DateTime $datetime;
3535
};
3636

@@ -44,7 +44,7 @@ public function it_can_resolve_a_global_cast()
4444
/** @test */
4545
public function it_can_resolve_a_global_cast_as_docblock()
4646
{
47-
$fake = new class {
47+
$fake = new class() {
4848
/** @var DateTime */
4949
public $datetime;
5050
};
@@ -59,7 +59,7 @@ public function it_can_resolve_a_global_cast_as_docblock()
5959
/** @test */
6060
public function it_can_have_no_type_and_no_cast()
6161
{
62-
$fake = new class {
62+
$fake = new class() {
6363
public $noType;
6464
};
6565

@@ -73,7 +73,7 @@ public function it_can_have_no_type_and_no_cast()
7373
/** @test */
7474
public function it_can_have_a_global_cast_with_an_array()
7575
{
76-
$fake = new class {
76+
$fake = new class() {
7777
/** @var \Spatie\LaravelSettings\Tests\TestClasses\DummyDto[] */
7878
public array $dto_array;
7979
};
@@ -88,7 +88,7 @@ public function it_can_have_a_global_cast_with_an_array()
8888
/** @test */
8989
public function it_can_have_a_global_cast_with_an_array_without_array_type()
9090
{
91-
$fake = new class {
91+
$fake = new class() {
9292
/** @var \Spatie\LaravelSettings\Tests\TestClasses\DummyDto[] */
9393
public $dto_array;
9494
};
@@ -103,7 +103,7 @@ public function it_can_have_a_global_cast_with_an_array_without_array_type()
103103
/** @test */
104104
public function it_can_have_a_plain_array_without_cast()
105105
{
106-
$fake = new class {
106+
$fake = new class() {
107107
public array $array;
108108
};
109109

@@ -117,7 +117,7 @@ public function it_can_have_a_plain_array_without_cast()
117117
/** @test */
118118
public function it_can_have_a_nullable_cast()
119119
{
120-
$fake = new class {
120+
$fake = new class() {
121121
public ?DateTime $array;
122122
};
123123

@@ -131,7 +131,7 @@ public function it_can_have_a_nullable_cast()
131131
/** @test */
132132
public function it_can_have_a_nullable_docblock_cast()
133133
{
134-
$fake = new class {
134+
$fake = new class() {
135135
/** @var ?\DateTime */
136136
public $array;
137137
};
@@ -148,7 +148,7 @@ public function it_can_create_a_local_cast_without_arguments()
148148
{
149149
$this->withoutGlobalCasts();
150150

151-
$fake = new class {
151+
$fake = new class() {
152152
public DateTime $datetime;
153153
};
154154

@@ -164,7 +164,7 @@ public function it_can_create_a_local_cast_without_arguments()
164164
/** @test */
165165
public function it_can_create_a_local_cast_with_class_identifier_and_arguments()
166166
{
167-
$fake = new class {
167+
$fake = new class() {
168168
public $dto;
169169
};
170170

@@ -180,7 +180,7 @@ public function it_can_create_a_local_cast_with_class_identifier_and_arguments()
180180
/** @test */
181181
public function it_can_create_a_local_cast_with_an_already_constructed_cast()
182182
{
183-
$fake = new class {
183+
$fake = new class() {
184184
public DummyDto $dto;
185185
};
186186

@@ -193,6 +193,29 @@ public function it_can_create_a_local_cast_with_an_already_constructed_cast()
193193
$this->assertEquals(new DtoCast(DummyDto::class), $cast);
194194
}
195195

196+
/** @test */
197+
public function it_will_not_resolve_a_cast_for_a_primitive_type()
198+
{
199+
$fake = new class() {
200+
/** @var int */
201+
public $int;
202+
203+
/** @var ?int */
204+
public $a_nullable_int;
205+
206+
/** @var int|null */
207+
public $another_nullable_int;
208+
209+
/** @var int[]|null */
210+
public $an_array_of_ints_or_null;
211+
};
212+
213+
$this->assertNull(SettingsCastFactory::resolve(new ReflectionProperty($fake, 'int'), []));
214+
$this->assertNull(SettingsCastFactory::resolve(new ReflectionProperty($fake, 'a_nullable_int'), []));
215+
$this->assertNull(SettingsCastFactory::resolve(new ReflectionProperty($fake, 'another_nullable_int'), []));
216+
$this->assertNull(SettingsCastFactory::resolve(new ReflectionProperty($fake, 'an_array_of_ints_or_null'), []));
217+
}
218+
196219
private function withoutGlobalCasts()
197220
{
198221
config()->set('settings.global_casts', []);

tests/SettingsContainerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SettingsContainerTest extends TestCase
1313
{
1414
private SettingsMigrator $migrator;
1515

16-
protected function setUp() : void
16+
protected function setUp(): void
1717
{
1818
parent::setUp();
1919

tests/SettingsRepositories/DatabaseSettingsRepositoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function () {
269269
},
270270
], [
271271
function () {
272-
$model = new class extends SettingsProperty {
272+
$model = new class() extends SettingsProperty {
273273
public function getConnectionName()
274274
{
275275
return 'other';

tests/SettingsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Spatie\LaravelSettings\Migrations\SettingsMigrator;
2323
use Spatie\LaravelSettings\Models\SettingsProperty;
2424
use Spatie\LaravelSettings\SettingsCache;
25+
use Spatie\LaravelSettings\Tests\TestClasses\DateSettings;
2526
use Spatie\LaravelSettings\Tests\TestClasses\DummyDto;
2627
use Spatie\LaravelSettings\Tests\TestClasses\DummyEncryptedSettings;
2728
use Spatie\LaravelSettings\Tests\TestClasses\DummySettings;

0 commit comments

Comments
 (0)