Skip to content

Commit

Permalink
Added PHPStan to verify code samples (#2370)
Browse files Browse the repository at this point in the history
* 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 <konrad.oboza@ibexa.co>

* 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 <konrad.oboza@ibexa.co>

* 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 <61695653+adriendupuis@users.noreply.github.com>

* PHP CS Fixes

* Removed TMP changes

---------

Co-authored-by: Konrad Oboza <konrad.oboza@ibexa.co>
Co-authored-by: Adrien Dupuis <61695653+adriendupuis@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 17, 2024
1 parent a516ddf commit be149fd
Show file tree
Hide file tree
Showing 94 changed files with 868 additions and 278 deletions.
118 changes: 81 additions & 37 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,62 +1,106 @@
name: 'Build & test documentation'
name: "Build & test documentation"

on:
push:
branches:
- master
- '[0-9]+.[0-9]+'
- "[0-9]+.[0-9]+"
pull_request: ~

jobs:
build:
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install php-cs-fixer
run: composer require friendsofphp/php-cs-fixer --dev
- name: Run PHP CS Fixer
run: ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: PHP CS Fixes
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run build
run: |
mkdocs build --strict
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- uses: "ramsey/composer-install@v3"
with:
working-directory: "tools/php-cs-fixer"
dependency-versions: highest

- name: Run PHP CS Fixer
run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: PHP CS Fixes

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run build
run: |
mkdocs build --strict
vale-check:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'

steps:
- uses: actions/checkout@v3
- name: Get Vale.sh configs
env:
TOKEN: ${{ secrets.EZROBOT_PAT }}
run: |
curl -H "Authorization: token $TOKEN" -L https://github.com/ibexa/vale-styles/archive/refs/heads/main.zip -o vale.zip
unzip vale.zip
rm vale.zip
mv vale-styles-main/* vale-styles-main/.vale.ini .
- name: Run Vale.sh
uses: errata-ai/vale-action@reviewdog
with:
- uses: actions/checkout@v4

- name: Get Vale.sh configs
env:
TOKEN: ${{ secrets.EZROBOT_PAT }}
run: |
curl -H "Authorization: token $TOKEN" -L https://github.com/ibexa/vale-styles/archive/refs/heads/main.zip -o vale.zip
unzip vale.zip
rm vale.zip
mv vale-styles-main/* vale-styles-main/.vale.ini .
- name: Run Vale.sh
uses: errata-ai/vale-action@reviewdog
with:
reporter: github-check
filter_mode: added

code-samples:
name: Validate code samples
runs-on: "ubuntu-22.04"
strategy:
matrix:
php:
- "8.3"
steps:
- uses: actions/checkout@v4

- name: Setup PHP Action
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
extensions: "pdo_sqlite, gd"
tools: cs2pr

- name: Add composer keys for private packagist
run: |
composer config http-basic.updates.ibexa.co $SATIS_NETWORK_KEY $SATIS_NETWORK_TOKEN
composer config github-oauth.github.com $TRAVIS_GITHUB_TOKEN
env:
SATIS_NETWORK_KEY: ${{ secrets.SATIS_NETWORK_KEY }}
SATIS_NETWORK_TOKEN: ${{ secrets.SATIS_NETWORK_TOKEN }}
TRAVIS_GITHUB_TOKEN: ${{ secrets.TRAVIS_GITHUB_TOKEN }}

- uses: ramsey/composer-install@v3
with:
dependency-versions: highest

- name: Run PHPStan analysis
run: composer phpstan
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__/*
**/.idea/
.php-cs-fixer.cache
composer.lock
tools/php-cs-fixer/vendor
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ mkdocs serve
After a short while your documentation should be reachable at http://localhost:8000. If it isn't, check the output
of the command.

## Testing the code samples

This repository uses PHPStan to test the code samples. To run the tests locally execute the commands below:
```bash
composer update
composer phpstan
```

## Where to View

https://doc.ibexa.co
22 changes: 19 additions & 3 deletions code_samples/api/commerce/src/Command/CartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace App\Command;

use Ibexa\Contracts\Cart\CartResolverInterface;
use Ibexa\Contracts\Cart\CartServiceInterface;
use Ibexa\Contracts\Cart\Value\CartCreateStruct;
use Ibexa\Contracts\Cart\Value\CartMetadataUpdateStruct;
use Ibexa\Contracts\Cart\Value\CartQuery;
use Ibexa\Contracts\Cart\Value\EntryAddStruct;
use Ibexa\Contracts\Cart\Value\EntryUpdateStruct;
use Ibexa\Contracts\Checkout\Reorder\ReorderService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\OrderManagement\OrderServiceInterface;
use Ibexa\Contracts\ProductCatalog\CurrencyServiceInterface;
use Ibexa\Contracts\ProductCatalog\ProductServiceInterface;
use Ibexa\Core\Repository\Permission\PermissionResolver;
Expand All @@ -30,18 +33,30 @@ final class CartCommand extends Command

private ProductServiceInterface $productService;

private OrderServiceInterface $orderService;

private ReorderService $reorderService;

private CartResolverInterface $cartResolver;

public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
CartServiceInterface $cartService,
CurrencyServiceInterface $currencyService,
ProductServiceInterface $productService
ProductServiceInterface $productService,
OrderServiceInterface $orderService,
ReorderService $reorderService,
CartResolverInterface $cartResolver
) {
$this->cartService = $cartService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->currencyService = $currencyService;
$this->productService = $productService;
$this->orderService = $orderService;
$this->reorderService = $reorderService;
$this->cartResolver = $cartResolver;

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

return self::SUCCESS;

// Get the order with items that should be reordered
$orderIdentifier = '2e897b31-0d7a-46d3-ba45-4eb65fe02790';
$order = $this->orderService->getOrderByIdentifier($orderIdentifier);

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

// Merge the carts into the target cart and delete the merged carts
$reorderCart = $this->cartService->mergeCarts($reorderCart, true, $existingCart);

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion code_samples/api/commerce/src/Command/OrderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$orderIdentifier = '2e897b31-0d7a-46d3-ba45-4eb65fe02790';
$order = $this->orderService->getOrderByIdentifier($orderIdentifier);

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

// Get order by id
$orderId = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Ibexa\Bundle\Core\Controller;
use Ibexa\Contracts\Cart\CartServiceInterface;
use Ibexa\Contracts\Checkout\CheckoutServiceInterface;
use Symfony\Component\HttpFoundation\Response;

class CustomCheckoutController extends Controller
{
Expand All @@ -20,7 +21,7 @@ public function __construct(CartServiceInterface $cartService, CheckoutServiceIn
$this->checkoutService = $checkoutService;
}

public function showContentAction()
public function showContentAction(): Response
{
// Get checkout for a specific cart
$cart = $this->cartService->getCart('d7424b64-7dc1-474c-82c8-1700f860d55e');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MyCustomTypeGraphQLCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('ibexa.graphql.schema.content.mapping.field_definition_type')) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
use Ibexa\Contracts\ProductCatalog\Values\Price\Create\Struct\ProductPriceCreateStruct;
use Ibexa\Contracts\ProductCatalog\Values\Price\PriceContext;
use Ibexa\Contracts\ProductCatalog\Values\Price\PriceQuery;
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Currency;
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Currency as CurrencyCriterion;
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\CustomerGroup;
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\LogicalOr;
use Ibexa\Contracts\ProductCatalog\Values\Price\Query\Criterion\Product;
use Money;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -90,23 +91,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

$prices = $this->productPriceService->findPricesByProductCode($productCode);
$prices = $this->productPriceService->findPricesByProductCode($productCode)->getPrices();

$output->writeln('All prices for ' . $product->getName() . ':');
foreach ($prices as $price) {
$output->writeln($price);
$output->writeln((string) $price);
}

$priceCriteria = [
new Currency('USD'),
new CurrencyCriterion($this->currencyService->getCurrencyByCode('USD')),
new CustomerGroup('customer_group_1'),
new Product('ergo_desk'),
];

$priceQuery = new PriceQuery(new LogicalOr(...$priceCriteria));
$prices = $this->priceService->findPrices($priceQuery);
$prices = $this->productPriceService->findPrices($priceQuery);

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

$context = new PriceContext($currency);
$price = $this->priceResolver->resolvePrice($product, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

$output->writeln($vatCategory->getVatValue());
$output->writeln((string) $vatCategory->getVatValue());

return self::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function __construct(LanguageService $languageService, UserService $userS
parent::__construct('doc:add_language');
}

protected function configure()
protected function configure(): void
{
$this->setDescription('Lists available languages and add Polish.');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$user = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(ContentService $contentService, LocationService $loc
parent::__construct('doc:add_location');
}

protected function configure()
protected function configure(): void
{
$this
->setDescription('Add a Location to content item and hides it.')
Expand All @@ -40,7 +40,7 @@ protected function configure()
]);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$user = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(BookmarkService $bookmarkService, LocationService $l
parent::__construct('doc:bookmark');
}

protected function configure()
protected function configure(): void
{
$this
->setDefinition([
Expand All @@ -32,7 +32,7 @@ protected function configure()
->addOption('delete', 'd', InputOption::VALUE_NONE, 'Delete the created bookmark?', null);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$locationId = $input->getArgument('locationId');
$location = $this->locationService->loadLocation($locationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(LocationService $locationService)
parent::__construct('doc:browse_locations');
}

protected function configure()
protected function configure(): void
{
$this
->setDescription('Lists all descendants of the Location')
Expand All @@ -28,7 +28,7 @@ protected function configure()
]);
}

private function browseLocation(Location $location, OutputInterface $output, $depth = 0)
private function browseLocation(Location $location, OutputInterface $output, int $depth = 0): void
{
$output->writeln($location->contentInfo->name);

Expand All @@ -38,7 +38,7 @@ private function browseLocation(Location $location, OutputInterface $output, $de
}
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$locationId = $input->getArgument('locationId');

Expand Down
Loading

0 comments on commit be149fd

Please sign in to comment.