Skip to content

Commit cc4032e

Browse files
committed
Fix CS
1 parent f5254ed commit cc4032e

33 files changed

+196
-84
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"scripts": {
3838
"ecs": "vendor/bin/ecs check src",
39+
"test": "vendor/bin/phpunit",
3940
"check-style": "phpcs src",
4041
"analyse": "vendor/bin/phpstan analyse src",
4142
"fix-style": "phpcbf src",

ecs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
$services->set(GeneralPhpdocAnnotationRemoveFixer::class)
4949
->call('configure', [['annotations' => ['copyright', 'category']]]);
5050

51-
$services->set(NoSuperfluousPhpdocTagsFixer::class);
51+
// $services->set(NoSuperfluousPhpdocTagsFixer::class);
5252

5353
$services->set(PhpdocOrderFixer::class);
5454

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ parameters:
22
level: 8
33
treatPhpDocTypesAsCertain: false
44
checkMissingIterableValueType: false
5+
checkGenericClassInNonGenericObjectType: false
56
inferPrivatePropertyTypeFromConstructor: true
67
reportUnmatchedIgnoredErrors: true # Could be set to false if necessary during PHPStan update
78
tmpDir: ./var/cache/phpstan
@@ -14,3 +15,6 @@ parameters:
1415
excludes_analyse:
1516

1617
ignoreErrors:
18+
-
19+
message: '#Unsafe usage of new static\(\)#'
20+
path: src/Data/Collection.php

script/src/CodeGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public static function generateResourcesFromSchema(SchemaCollection $schemaColle
4242
$entityPath = sprintf("%s/%sEntity.php", $folderPath, $entityName);
4343

4444
if (is_dir(sprintf(__DIR__ . $outputEntityDir . "%s", $entityName)) && file_exists($entityPath)) {
45+
static::$definitionMapping[$entity->entity] = $entityNamespace . "\\" . $entityName . "\\" . $entityName . 'Definition';
46+
4547
continue;
4648
}
4749

@@ -50,8 +52,9 @@ public static function generateResourcesFromSchema(SchemaCollection $schemaColle
5052
file_put_contents($entityPath, static::generateEntityClass($entityNamespace, $entityName, $entity->properties));
5153
file_put_contents(sprintf("%s/%sCollection.php", $folderPath, $entityName), static::generateCollectionClass($entityNamespace, $entityName));
5254
file_put_contents(sprintf("%s/%sDefinition.php", $folderPath, $entityName), static::generateDefinitionClass($entityNamespace, $entityName, $entity->entity));
53-
file_put_contents(__DIR__ . $resourcePath . "entity-mapping.json", json_encode(static::$definitionMapping));
5455
}
56+
57+
file_put_contents(__DIR__ . $resourcePath . "entity-mapping.json", json_encode(static::$definitionMapping));
5558
}
5659

5760
private static function generateEntityClass(string $entityNamespace, string $entityName, PropertyCollection $propertyCollection): string
@@ -75,7 +78,10 @@ private static function generateEntityClass(string $entityNamespace, string $ent
7578

7679
if ($type) {
7780
$dummyProperty->setType($type);
81+
} else {
82+
$dummyProperty->setDocBlock('@var mixed');
7883
}
84+
7985
$dummyProperty->setVisibility(PropertyGenerator::FLAG_PUBLIC);
8086
$properties[] = $dummyProperty;
8187
}

src/Client/AdminAuthenticator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class AdminAuthenticator
1818

1919
public const OAUTH_TOKEN_ENDPOINT = '/api/oauth/token';
2020

21-
public static $headers = [
21+
public static array $headers = [
2222
'Content-Type' => 'application/x-www-form-urlencoded'
2323
];
2424

2525
/**
26-
* @var callable
26+
* @var callable|null
2727
*/
2828
private $tokenCallback;
2929

@@ -120,7 +120,7 @@ private function getFullUrl(string $path): string
120120
return $this->endpoint . $path;
121121
}
122122

123-
public function getTokenCallback(): callable
123+
public function getTokenCallback(): ?callable
124124
{
125125
return $this->tokenCallback;
126126
}

src/Data/Collection.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@
44

55
abstract class Collection extends Struct implements \IteratorAggregate, \Countable
66
{
7-
protected iterable $elements = [];
7+
protected array $elements = [];
88

99
public function __construct(iterable $elements = [])
1010
{
11+
$elements = $elements instanceof \Traversable ? iterator_to_array($elements) : (array) $elements;
12+
1113
foreach ($elements as $key => $element) {
1214
$this->set($key, $element);
1315
}
1416
}
1517

18+
/**
19+
* @param mixed|null $element
20+
*/
1621
public function add($element): void
1722
{
1823
$this->validateType($element);
1924

2025
$this->elements[] = $element;
2126
}
2227

28+
/**
29+
* @param mixed|null $element
30+
* @param mixed|null $key
31+
*/
2332
public function set($key, $element): void
2433
{
2534
$this->validateType($element);
@@ -60,6 +69,9 @@ public function getKeys(): array
6069
return array_keys($this->elements);
6170
}
6271

72+
/**
73+
* @param mixed|null $key
74+
*/
6375
public function has($key): bool
6476
{
6577
return \array_key_exists($key, $this->elements);
@@ -70,6 +82,10 @@ public function map(\Closure $closure): array
7082
return array_map($closure, $this->elements);
7183
}
7284

85+
/**
86+
* @param mixed|null $initial
87+
* @return mixed
88+
*/
7389
public function reduce(\Closure $closure, $initial = null)
7490
{
7591
return array_reduce($this->elements, $closure, $initial);
@@ -88,7 +104,7 @@ public function sort(\Closure $closure): void
88104
/**
89105
* @return static
90106
*/
91-
public function filterInstance(string $class): self
107+
public function filterInstance(string $class)
92108
{
93109
return $this->filter(static function ($item) use ($class) {
94110
return $item instanceof $class;
@@ -98,7 +114,7 @@ public function filterInstance(string $class): self
98114
/**
99115
* @return static
100116
*/
101-
public function filter(\Closure $closure): self
117+
public function filter(\Closure $closure)
102118
{
103119
return $this->createNew(array_filter($this->elements, $closure));
104120
}
@@ -111,7 +127,7 @@ public function slice(int $offset, ?int $length = null)
111127
return $this->createNew(\array_slice($this->elements, $offset, $length, true));
112128
}
113129

114-
public function getElements(): array
130+
public function getElements(): iterable
115131
{
116132
return $this->elements;
117133
}
@@ -123,11 +139,14 @@ public function jsonSerialize(): array
123139
}, $this->elements));
124140
}
125141

126-
public function first()
142+
public function first(): ?Struct
127143
{
128144
return array_values($this->elements)[0] ?? null;
129145
}
130146

147+
/**
148+
* @return mixed
149+
*/
131150
public function last()
132151
{
133152
return array_values($this->elements)[\count($this->elements) - 1] ?? null;
@@ -159,6 +178,9 @@ protected function createNew(iterable $elements = [])
159178
return new static($elements);
160179
}
161180

181+
/**
182+
* @param mixed $element
183+
*/
162184
protected function validateType($element): void
163185
{
164186
$expectedClass = $this->getExpectedClass();

src/Data/Criteria.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Criteria implements ParseAware
7272
private array $sortings = [];
7373

7474
/**
75-
* @var FilterAggregation[]
75+
* @var Aggregation[]
7676
*/
7777
private array $aggregations = [];
7878

@@ -283,6 +283,7 @@ public static function range(string $field, array $range): RangeFilter
283283
}
284284

285285
/**
286+
* @param mixed $value
286287
*/
287288
public static function equals(string $field, $value): EqualsFilter
288289
{

src/Data/Entity/Customer/CustomerEntity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class CustomerEntity extends Entity
117117

118118
public ?CustomerRecoveryEntity $recoveryCustomer = null;
119119

120+
/**
121+
* @var mixed
122+
*/
120123
public $remoteAddress = null;
121124

122125
public ?array $tagIds = null;

src/Data/Entity/Entity.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,46 @@ public function has(string $property): bool
3131
return property_exists($this, $property);
3232
}
3333

34+
/**
35+
* @param mixed $value
36+
*/
3437
public function setProperty(string $property, $value): void
3538
{
36-
if (!$this->has($property) || $value === null) {
39+
if (!$this->has($property)) {
3740
$this->$property = $value;
3841
return;
3942
}
4043

4144
$rp = new \ReflectionProperty($this, $property);
4245

46+
/** @var \ReflectionNamedType|null $type */
4347
$type = $rp->getType();
4448

4549
if ($type === null || ($type->allowsNull() && $value === null)) {
4650
$this->$property = $value;
4751
return;
4852
}
4953

54+
/** @var class-string $typeName */
5055
$typeName = $type->getName();
5156

52-
if ($value instanceof EntityCollection || $value instanceof Entity) {
57+
if (in_array($typeName, self::NON_STRUCT_PROPERTY_TYPES)) {
58+
$this->$property = $value;
59+
return;
60+
}
61+
62+
if (is_subclass_of($typeName, Struct::class) && ($value instanceof EntityCollection || $value instanceof Entity)) {
5363
$original = $value;
5464
$value = $typeName::createFrom($value);
5565

56-
if (method_exists($original, 'internalSetEntityName')) {
57-
$value->internalSetEntityName($original->getEntityName());
66+
if ($original instanceof Entity && $value instanceof Entity && $entity = $original->getEntityName()) {
67+
$value->internalSetEntityName($entity);
5868
}
5969

6070
$this->$property = $value;
6171
return;
6272
}
6373

64-
if (in_array($typeName, self::NON_STRUCT_PROPERTY_TYPES, true)) {
65-
$this->$property = $value;
66-
return;
67-
}
68-
6974
$reflectionClass = new \ReflectionClass($typeName);
7075

7176
$dummyType = $reflectionClass->isInstantiable() ? new $typeName : null;
@@ -109,6 +114,9 @@ public function getTranslated(): array
109114
return $this->translated;
110115
}
111116

117+
/**
118+
* @return mixed
119+
*/
112120
public function getTranslation(string $field)
113121
{
114122
return $this->translated[$field] ?? null;
@@ -119,6 +127,9 @@ public function setTranslated(array $translated): void
119127
$this->translated = $translated;
120128
}
121129

130+
/**
131+
* @param mixed $value
132+
*/
122133
public function addTranslated(string $key, $value): void
123134
{
124135
$this->translated[$key] = $value;
@@ -136,7 +147,7 @@ public function getEntityName(): ?string
136147
return $this->_entityName;
137148
}
138149

139-
public function assignProperties(array $options)
150+
public function assignProperties(array $options): self
140151
{
141152
foreach ($options as $key => $value) {
142153
if ($key === 'id' && property_exists($this, 'id')) {

src/Data/Entity/EntityCollection.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
use Vin\ShopwareSdk\Data\Collection;
88

9+
/**
10+
* @method void add(Entity $entity)
11+
* @method void set(string $key, Entity $entity)
12+
* @method EntityCollection filter(\Closure $closure)()
13+
* @method EntityCollection createNew(iterable $elements = [])
14+
* @method Entity[] getIterator()
15+
* @method Entity[] getElements()
16+
* @method Entity|null get(string $key)
17+
* @method Entity|null first()
18+
* @method Entity|null last()
19+
*/
920
class EntityCollection extends Collection
1021
{
1122
public function getIds(): array
@@ -15,7 +26,10 @@ public function getIds(): array
1526
});
1627
}
1728

18-
public function filterByProperty(string $property, $value)
29+
/**
30+
* @param mixed $value
31+
*/
32+
public function filterByProperty(string $property, $value): self
1933
{
2034
return $this->filter(
2135
static function (Entity $struct) use ($property, $value) {
@@ -24,12 +38,15 @@ static function (Entity $struct) use ($property, $value) {
2438
);
2539
}
2640

27-
public function filterAndReduceByProperty(string $property, $value)
41+
/**
42+
* @param mixed $value
43+
*/
44+
public function filterAndReduceByProperty(string $property, $value): self
2845
{
2946
$filtered = [];
3047

3148
foreach ($this->getIterator() as $key => $struct) {
32-
if ($struct->get($property) !== $value) {
49+
if ($struct->$property !== $value) {
3350
continue;
3451
}
3552
$filtered[] = $struct;

src/Data/Entity/OrderCustomer/OrderCustomerEntity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@ class OrderCustomerEntity extends Entity
4141

4242
public ?SalutationEntity $salutation = null;
4343

44+
/**
45+
* @var mixed|null $remoteAddress
46+
*/
4447
public $remoteAddress = null;
4548
}

src/Data/Entity/Webhook/WebhookEntity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class WebhookEntity extends Entity
1717

1818
public ?string $url = null;
1919

20-
public ?string $appId = null;
21-
22-
public ?AppEntity $app = null;
23-
2420
public ?int $errorCount = null;
2521

2622
public ?bool $active = null;
23+
24+
public ?string $appId = null;
25+
26+
public ?AppEntity $app = null;
2727
}

src/Data/Filter/EqualsFilter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ class EqualsFilter extends Filter
88
{
99
private string $field;
1010

11+
/**
12+
* @var mixed
13+
*/
1114
private $value;
1215

1316
/**
14-
* EqualsFilter constructor.
17+
* @param mixed $value
1518
*/
1619
public function __construct(string $field, $value)
1720
{

0 commit comments

Comments
 (0)