Skip to content

Commit be149fd

Browse files
mnoconkonradobozaadriendupuis
authored
Added PHPStan to verify code samples (#2370)
* Applied CI review suggestion * Added basic PHPStan config * Added PHPStan for ActivityLog examples * Added CI * [TMP] Added invalid code on purpose * Fixed auth issues * Fixed CS job * PHP CS Fixes * Rerun * Simplified namespaces * [TMP] Use the old namespace * Generated baseline * Reduced baseline * Finished reviewing baseline * PHP CS Fixes * Rerun * Removed autoloading * [TMP] Break * Adjusted lines * Apply suggestions from code review Co-authored-by: Konrad Oboza <[email protected]> * Added space separation * Line offset fixes (#2430) * checkout_api.md: Fix included lines after PHPStan fixes * price_api.md: Fix included lines after PHPStan fixes The confirmation output at the end of "Resolve prices" example was missing. * create_custom_attribute_type.md: Fix highlighted line Two lines removed in 086b885 but the highlighted line wasn't updated. * search_api.md: Use `ContentTypeTermAggregation` lines * oauth_client.md: Fix PROVIDER_PREFIX highlight * segment_api.md: Fix highlighted line One line added in aeee20a but the highlighted line wasn't updated. (renaming from docs/api/public_php_api_managing_users.md to docs/users/segment_api.md in a9031bd) * segment_api.md: Fix highlighted lines One line added in aeee20a but the highlighted line wasn't updated. (renaming from docs/api/public_php_api_managing_users.md to docs/users/segment_api.md in a9031bd) * segment_api.md: Fix loadSegmentByIdentifier example intro * search_api.md: Fix #sorting-results offset * cart_api.md: Add missing EOF new line * Added basic PHPStan config * Added PHPStan for ActivityLog examples * Added CI * [TMP] Added invalid code on purpose * Fixed auth issues * Fixed CS job * PHP CS Fixes * Rerun * Simplified namespaces * [TMP] Use the old namespace * Generated baseline * Reduced baseline * Finished reviewing baseline * PHP CS Fixes * Rerun * Removed autoloading * [TMP] Break * Adjusted lines * Applied CI review suggestion * Apply suggestions from code review Co-authored-by: Konrad Oboza <[email protected]> * Added space separation * Revert "[TMP] Break" This reverts commit ac2cfbb. * Bumped CS-Fixer version * Fixed CS * Removed redundant doc block from WebinarEventTitleFulltextFieldMapper * Added readme mention * Added empty line to cart_api * Removed CS-fixer dependencies * Improved cart_api * Update code_samples/recent_activity/src/Command/MonitorRecentContentCreationCommand.php Co-authored-by: Adrien Dupuis <[email protected]> * PHP CS Fixes * Removed TMP changes --------- Co-authored-by: Konrad Oboza <[email protected]> Co-authored-by: Adrien Dupuis <[email protected]>
1 parent a516ddf commit be149fd

File tree

94 files changed

+868
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+868
-278
lines changed

.github/workflows/build.yaml

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,106 @@
1-
name: 'Build & test documentation'
1+
name: "Build & test documentation"
22

33
on:
44
push:
55
branches:
66
- master
7-
- '[0-9]+.[0-9]+'
7+
- "[0-9]+.[0-9]+"
88
pull_request: ~
99

1010
jobs:
1111
build:
1212
permissions:
13-
# Give the default GITHUB_TOKEN write permission to commit and push the
14-
# added or changed files to the repository.
15-
contents: write
13+
# Give the default GITHUB_TOKEN write permission to commit and push the
14+
# added or changed files to the repository.
15+
contents: write
1616

1717
runs-on: ubuntu-latest
1818
strategy:
1919
matrix:
2020
python-version: [3.8]
2121

2222
steps:
23-
- uses: actions/checkout@v3
24-
- name: Set up Python ${{ matrix.python-version }}
25-
uses: actions/setup-python@v3
26-
with:
27-
python-version: ${{ matrix.python-version }}
28-
- name: Install php-cs-fixer
29-
run: composer require friendsofphp/php-cs-fixer --dev
30-
- name: Run PHP CS Fixer
31-
run: ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots
32-
- name: Commit changes
33-
uses: stefanzweifel/git-auto-commit-action@v4
34-
with:
35-
commit_message: PHP CS Fixes
36-
- name: Install dependencies
37-
run: |
38-
python -m pip install --upgrade pip
39-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
40-
- name: Run build
41-
run: |
42-
mkdocs build --strict
23+
- uses: actions/checkout@v4
24+
25+
- name: Set up Python ${{ matrix.python-version }}
26+
uses: actions/setup-python@v3
27+
with:
28+
python-version: ${{ matrix.python-version }}
29+
30+
- uses: "ramsey/composer-install@v3"
31+
with:
32+
working-directory: "tools/php-cs-fixer"
33+
dependency-versions: highest
34+
35+
- name: Run PHP CS Fixer
36+
run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots
37+
38+
- name: Commit changes
39+
uses: stefanzweifel/git-auto-commit-action@v4
40+
with:
41+
commit_message: PHP CS Fixes
42+
43+
- name: Install dependencies
44+
run: |
45+
python -m pip install --upgrade pip
46+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
47+
48+
- name: Run build
49+
run: |
50+
mkdocs build --strict
4351
4452
vale-check:
4553
runs-on: ubuntu-latest
4654
if: github.event_name == 'pull_request'
4755

4856
steps:
49-
- uses: actions/checkout@v3
50-
- name: Get Vale.sh configs
51-
env:
52-
TOKEN: ${{ secrets.EZROBOT_PAT }}
53-
run: |
54-
curl -H "Authorization: token $TOKEN" -L https://github.com/ibexa/vale-styles/archive/refs/heads/main.zip -o vale.zip
55-
unzip vale.zip
56-
rm vale.zip
57-
mv vale-styles-main/* vale-styles-main/.vale.ini .
58-
- name: Run Vale.sh
59-
uses: errata-ai/vale-action@reviewdog
60-
with:
57+
- uses: actions/checkout@v4
58+
59+
- name: Get Vale.sh configs
60+
env:
61+
TOKEN: ${{ secrets.EZROBOT_PAT }}
62+
run: |
63+
curl -H "Authorization: token $TOKEN" -L https://github.com/ibexa/vale-styles/archive/refs/heads/main.zip -o vale.zip
64+
unzip vale.zip
65+
rm vale.zip
66+
mv vale-styles-main/* vale-styles-main/.vale.ini .
67+
68+
- name: Run Vale.sh
69+
uses: errata-ai/vale-action@reviewdog
70+
with:
6171
reporter: github-check
6272
filter_mode: added
73+
74+
code-samples:
75+
name: Validate code samples
76+
runs-on: "ubuntu-22.04"
77+
strategy:
78+
matrix:
79+
php:
80+
- "8.3"
81+
steps:
82+
- uses: actions/checkout@v4
83+
84+
- name: Setup PHP Action
85+
uses: shivammathur/setup-php@v2
86+
with:
87+
php-version: ${{ matrix.php }}
88+
coverage: none
89+
extensions: "pdo_sqlite, gd"
90+
tools: cs2pr
91+
92+
- name: Add composer keys for private packagist
93+
run: |
94+
composer config http-basic.updates.ibexa.co $SATIS_NETWORK_KEY $SATIS_NETWORK_TOKEN
95+
composer config github-oauth.github.com $TRAVIS_GITHUB_TOKEN
96+
env:
97+
SATIS_NETWORK_KEY: ${{ secrets.SATIS_NETWORK_KEY }}
98+
SATIS_NETWORK_TOKEN: ${{ secrets.SATIS_NETWORK_TOKEN }}
99+
TRAVIS_GITHUB_TOKEN: ${{ secrets.TRAVIS_GITHUB_TOKEN }}
100+
101+
- uses: ramsey/composer-install@v3
102+
with:
103+
dependency-versions: highest
104+
105+
- name: Run PHPStan analysis
106+
run: composer phpstan

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ __pycache__/*
55
**/.idea/
66
.php-cs-fixer.cache
77
composer.lock
8+
tools/php-cs-fixer/vendor

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ mkdocs serve
5252
After a short while your documentation should be reachable at http://localhost:8000. If it isn't, check the output
5353
of the command.
5454

55+
## Testing the code samples
56+
57+
This repository uses PHPStan to test the code samples. To run the tests locally execute the commands below:
58+
```bash
59+
composer update
60+
composer phpstan
61+
```
62+
5563
## Where to View
5664

5765
https://doc.ibexa.co

code_samples/api/commerce/src/Command/CartCommand.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace App\Command;
66

7+
use Ibexa\Contracts\Cart\CartResolverInterface;
78
use Ibexa\Contracts\Cart\CartServiceInterface;
89
use Ibexa\Contracts\Cart\Value\CartCreateStruct;
910
use Ibexa\Contracts\Cart\Value\CartMetadataUpdateStruct;
1011
use Ibexa\Contracts\Cart\Value\CartQuery;
1112
use Ibexa\Contracts\Cart\Value\EntryAddStruct;
1213
use Ibexa\Contracts\Cart\Value\EntryUpdateStruct;
14+
use Ibexa\Contracts\Checkout\Reorder\ReorderService;
1315
use Ibexa\Contracts\Core\Repository\UserService;
16+
use Ibexa\Contracts\OrderManagement\OrderServiceInterface;
1417
use Ibexa\Contracts\ProductCatalog\CurrencyServiceInterface;
1518
use Ibexa\Contracts\ProductCatalog\ProductServiceInterface;
1619
use Ibexa\Core\Repository\Permission\PermissionResolver;
@@ -30,18 +33,30 @@ final class CartCommand extends Command
3033

3134
private ProductServiceInterface $productService;
3235

36+
private OrderServiceInterface $orderService;
37+
38+
private ReorderService $reorderService;
39+
40+
private CartResolverInterface $cartResolver;
41+
3342
public function __construct(
3443
PermissionResolver $permissionResolver,
3544
UserService $userService,
3645
CartServiceInterface $cartService,
3746
CurrencyServiceInterface $currencyService,
38-
ProductServiceInterface $productService
47+
ProductServiceInterface $productService,
48+
OrderServiceInterface $orderService,
49+
ReorderService $reorderService,
50+
CartResolverInterface $cartResolver
3951
) {
4052
$this->cartService = $cartService;
4153
$this->permissionResolver = $permissionResolver;
4254
$this->userService = $userService;
4355
$this->currencyService = $currencyService;
4456
$this->productService = $productService;
57+
$this->orderService = $orderService;
58+
$this->reorderService = $reorderService;
59+
$this->cartResolver = $cartResolver;
4560

4661
parent::__construct('doc:cart');
4762
}
@@ -134,9 +149,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134149
// Delete a cart permanently
135150
$this->cartService->deleteCart($cart);
136151

137-
return self::SUCCESS;
138-
139152
// Get the order with items that should be reordered
153+
$orderIdentifier = '2e897b31-0d7a-46d3-ba45-4eb65fe02790';
140154
$order = $this->orderService->getOrderByIdentifier($orderIdentifier);
141155

142156
// Get the cart to merge
@@ -148,5 +162,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
148162

149163
// Merge the carts into the target cart and delete the merged carts
150164
$reorderCart = $this->cartService->mergeCarts($reorderCart, true, $existingCart);
165+
166+
return self::SUCCESS;
151167
}
152168
}

code_samples/api/commerce/src/Command/OrderCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5858
$orderIdentifier = '2e897b31-0d7a-46d3-ba45-4eb65fe02790';
5959
$order = $this->orderService->getOrderByIdentifier($orderIdentifier);
6060

61-
$output->writeln(sprintf('Order has status %s', $orderIdentifier, $order->getStatus()));
61+
$output->writeln(sprintf('Order %s has status %s', $orderIdentifier, $order->getStatus()));
6262

6363
// Get order by id
6464
$orderId = 1;

code_samples/api/commerce/src/Controller/CustomCheckoutController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ibexa\Bundle\Core\Controller;
88
use Ibexa\Contracts\Cart\CartServiceInterface;
99
use Ibexa\Contracts\Checkout\CheckoutServiceInterface;
10+
use Symfony\Component\HttpFoundation\Response;
1011

1112
class CustomCheckoutController extends Controller
1213
{
@@ -20,7 +21,7 @@ public function __construct(CartServiceInterface $cartService, CheckoutServiceIn
2021
$this->checkoutService = $checkoutService;
2122
}
2223

23-
public function showContentAction()
24+
public function showContentAction(): Response
2425
{
2526
// Get checkout for a specific cart
2627
$cart = $this->cartService->getCart('d7424b64-7dc1-474c-82c8-1700f860d55e');

code_samples/api/graphql/src/DependencyInjection/Compiler/MyCustomTypeGraphQLCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class MyCustomTypeGraphQLCompilerPass implements CompilerPassInterface
99
{
10-
public function process(ContainerBuilder $container)
10+
public function process(ContainerBuilder $container): void
1111
{
1212
if (!$container->hasParameter('ibexa.graphql.schema.content.mapping.field_definition_type')) {
1313
return;

code_samples/api/product_catalog/src/Command/ProductPriceCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use Ibexa\Contracts\ProductCatalog\Values\Price\Create\Struct\ProductPriceCreateStruct;
1212
use Ibexa\Contracts\ProductCatalog\Values\Price\PriceContext;
1313
use Ibexa\Contracts\ProductCatalog\Values\Price\PriceQuery;
14-
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Currency;
14+
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Currency as CurrencyCriterion;
1515
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\CustomerGroup;
16+
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\LogicalOr;
1617
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Product;
1718
use Money;
1819
use Symfony\Component\Console\Command\Command;
@@ -90,23 +91,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9091

9192
$output->writeln('Created new price in currency ' . $newCurrencyCode);
9293

93-
$prices = $this->productPriceService->findPricesByProductCode($productCode);
94+
$prices = $this->productPriceService->findPricesByProductCode($productCode)->getPrices();
9495

9596
$output->writeln('All prices for ' . $product->getName() . ':');
9697
foreach ($prices as $price) {
97-
$output->writeln($price);
98+
$output->writeln((string) $price);
9899
}
99100

100101
$priceCriteria = [
101-
new Currency('USD'),
102+
new CurrencyCriterion($this->currencyService->getCurrencyByCode('USD')),
102103
new CustomerGroup('customer_group_1'),
103104
new Product('ergo_desk'),
104105
];
105106

106107
$priceQuery = new PriceQuery(new LogicalOr(...$priceCriteria));
107-
$prices = $this->priceService->findPrices($priceQuery);
108+
$prices = $this->productPriceService->findPrices($priceQuery);
108109

109-
$output->writeln(sprintf('Found %d prices with provided criteria', count($prices)));
110+
$output->writeln(sprintf('Found %d prices with provided criteria', $prices->getTotalCount()));
110111

111112
$context = new PriceContext($currency);
112113
$price = $this->priceResolver->resolvePrice($product, $context);

code_samples/api/product_catalog/src/Command/VatCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5757

5858
$vatCategory = $this->vatService->getVatCategoryByIdentifier($region, 'reduced');
5959

60-
$output->writeln($vatCategory->getVatValue());
60+
$output->writeln((string) $vatCategory->getVatValue());
6161

6262
return self::SUCCESS;
6363
}

code_samples/api/public_php_api/src/Command/AddLanguageCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function __construct(LanguageService $languageService, UserService $userS
2525
parent::__construct('doc:add_language');
2626
}
2727

28-
protected function configure()
28+
protected function configure(): void
2929
{
3030
$this->setDescription('Lists available languages and add Polish.');
3131
}
3232

33-
protected function execute(InputInterface $input, OutputInterface $output)
33+
protected function execute(InputInterface $input, OutputInterface $output): int
3434
{
3535
$user = $this->userService->loadUserByLogin('admin');
3636
$this->permissionResolver->setCurrentUserReference($user);

0 commit comments

Comments
 (0)