Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions resources/guidelines/troubleshooting/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
nav:
title: Troubleshooting
position: 10

---

# Troubleshooting

Use this section to diagnose and resolve common issues you might encounter while working with Shopware projects.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
nav:
title: Troubleshooting
position: 80
title: Performance
position: 20

---

# Troubleshooting
# Performance

## Performance
## Common Performance Considerations

### Dynamic product groups are slow to load

Expand Down
125 changes: 125 additions & 0 deletions resources/guidelines/troubleshooting/phpstan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
nav:
title: PHPStan
position: 30
---

# PHPStan

## Common PHPStan Issues in Shopware Code

### EntityRepository Should Define a Generic Type

**Problem**: Repository returns EntityCollection without type information.

```php

Check warning on line 15 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L15

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:15:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
$products = $this->productRepository->search($criteria, $context)->getEntities();
foreach ($products as $product) {
// PHPStan doesn't know $product is ProductEntity
$name = $product->getName(); // Call to an undefined method Shopware\Core\Framework\DataAbstractionLayer\Entity::getName()
}
```

**Solution**: Add a PHP doc with a generic type to EntityRepository:

```php

Check warning on line 25 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L25

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:25:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
class Foo
{
/**
* @param EntityRepository<ProductCollection> $productRepository
*/
public function __construct(
private readonly EntityRepository $productRepository,
) {
}

public function doSomething(): void
{
// ...
$products = $this->productRepository->search($criteria, $context)->getEntities();
foreach ($products as $product) {
$name = $product->getName(); // PHPStan correctly identifies this as ProductEntity
}
}
}
```

Be aware that the `EntityRepository` class is a generic class, which gets an EntityCollection as type.
This might sound counter-intuitive and different to other well-known repository classes, which take the Entity class as the generic type.
But it was the easiest technical solution to get PHPStan to understand the type of the collection returned by the search method.

### Null Safety with First method and Associations

**Problem**: Calling `first` could return `null`, also entity associations can be `null` if not loaded.

```php

Check warning on line 55 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L55

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:55:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
$product = $this->productRepository->search($criteria, $context)->first();
$manufacturer = $product->getManufacturer(); // Cannot call method getManufacturer() on Shopware\Core\Content\Product\ProductEntity|null.
$manufacturerName = $manufacturer->getName(); // Cannot call method getName() on Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerEntity|null.
```

**Solution**: Ensure associations are added before in the criteria and always check for possible `null` returns:

```php

Check warning on line 63 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L63

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:63:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
$criteria = new Criteria();
$criteria->addAssociation('manufacturer');

$product = $this->productRepository->search($criteria, $context)->first();
if ($product === null) {
throw new ProductNotFoundException();
}

$manufacturer = $product->getManufacturer();
if ($manufacturer === null) {
throw new ManufacturerNotLoadedException();
}

$manufacturerName = $manufacturer->getName(); // No error
```

Or use the null-safe operators:

```php

Check warning on line 82 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L82

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:82:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
$manufacturerName = $product?->getManufacturer()?->getName() ?? 'Unknown';
```

### Missing Generic Type for EntityCollection

**Problem**: Custom EntityCollection does not have a generic type.

```php

Check warning on line 90 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L90

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:90:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
class FooCollection extends EntityCollection
{
protected function getExpectedClass(): string
{
return FooEntity::class;
}
}

$foo = $fooCollection->first();
if ($foo === null) {
throw new FooNotFoundException();
}
$foo->bar(); // Cannot call method bar() on Shopware\Core\Framework\DataAbstractionLayer\Entity.
```

**Solution**: Add a generic type to EntityCollection:

```php

Check warning on line 108 in resources/guidelines/troubleshooting/phpstan.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] resources/guidelines/troubleshooting/phpstan.md#L108

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/guidelines/troubleshooting/phpstan.md:108:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
/**
* @extends EntityCollection<FooEntity>
*/
class FooCollection extends EntityCollection
{
protected function getExpectedClass(): string
{
return FooEntity::class;
}
}

$foo = $fooCollection->first();
if ($foo === null) {
throw new FooNotFoundException();
}
$foo->bar(); // No error
```