Skip to content

Commit

Permalink
Implement new quality standard
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Jul 24, 2023
1 parent 5696fe7 commit 1a7f290
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 118 deletions.
106 changes: 57 additions & 49 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
name: 'CI'
name: 'Static Analysis'

on:
pull_request: ~
push:
branches:
- 'master'
pull_request: ~
push:
branches:
- 'master'

jobs:
tests:
runs-on: 'ubuntu-latest'

strategy:
matrix:
php-version:
- '7.4'

steps:
- name: 'Checkout'
uses: 'actions/checkout@v3'

- name: 'Install PHP'
uses: 'shivammathur/setup-php@v2'
with:
php-version: '${{ matrix.php-version }}'
coverage: 'none'
tools: 'composer:v2'

- name: 'Get composer cache directory'
id: 'composercache'
run: 'echo "::set-output name=dir::$(composer config cache-files-dir)"'

- name: 'Cache dependencies'
uses: 'actions/cache@v3'
with:
path: '${{ steps.composercache.outputs.dir }}'
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: '${{ runner.os }}-composer-'

- name: 'Prepare credentials'
env:
MAGENTO_USERNAME: '${{ secrets.MAGENTO_USERNAME }}'
MAGENTO_PASSWORD: '${{ secrets.MAGENTO_PASSWORD }}'
run: 'composer config -g http-basic.repo.magento.com "$MAGENTO_USERNAME" "$MAGENTO_PASSWORD"'

- name: 'Install dependencies'
run: 'composer install --prefer-dist'

- name: 'Run PHP CodeSniffer'
run: 'vendor/bin/phpcs --extensions=php,phtml'

- name: 'Run PHPMD'
run: 'vendor/bin/phpmd . xml phpmd.xml.dist'
tests:
runs-on: 'ubuntu-latest'

steps:
- name: 'Checkout'
uses: 'actions/checkout@v3'

- name: 'Install PHP'
uses: 'shivammathur/setup-php@v2'
with:
php-version: '7.4'
coverage: 'none'
tools: 'composer:v2'
env:
COMPOSER_AUTH_JSON: |
{
"http-basic": {
"repo.magento.com": {
"username": "${{ secrets.MAGENTO_USERNAME }}",
"password": "${{ secrets.MAGENTO_PASSWORD }}"
}
}
}
- name: 'Get composer cache directory'
id: 'composer-cache'
run: 'echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT'

- name: 'Cache dependencies'
uses: 'actions/cache@v3'
with:
path: '${{ steps.composer-cache.outputs.dir }}'
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: '${{ runner.os }}-composer-'

- name: 'Install dependencies'
run: 'composer install --prefer-dist'

- name: 'Run composer audit'
run: 'composer audit --format=plain'

- name: 'Run Parallel Lint'
run: 'vendor/bin/parallel-lint --exclude vendor .'

- name: 'Run PHP CodeSniffer'
run: 'vendor/bin/phpcs --extensions=php,phtml'

- name: 'Run PHPMD'
run: 'vendor/bin/phpmd . xml phpmd.xml.dist'

- name: 'Run PHPStan'
run: 'vendor/bin/phpstan analyse'
5 changes: 3 additions & 2 deletions Api/CustomEntityRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResults;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand Down Expand Up @@ -41,11 +42,11 @@ public function get(int $entityId, ?int $storeId = null, bool $forceReload = fal
/**
* Delete custom entity.
*
* @param CustomEntityInterface $entity Deleted entity.
* @param CustomEntityInterface|DataObject $entity Deleted entity.
* @return bool Will returned True if deleted
* @throws StateException
*/
public function delete(CustomEntityInterface $entity): bool;
public function delete($entity): bool;

/**
* Delete custom entity by id.
Expand Down
13 changes: 5 additions & 8 deletions Api/Data/CustomEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ interface CustomEntityInterface extends EntityInterface, IdentityInterface
*/
public const ENTITY = 'smile_custom_entity';

/**#@+
/**
* Constants defined for keys of data array
*/
public const URL_KEY = 'url_key';
/**#@-*/

/**
* Returns custom entity url key.
Expand All @@ -45,17 +44,15 @@ public function getUrlPath(): ?string;
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return \Smile\CustomEntity\Api\Data\CustomEntityExtensionInterface|null
* @return CustomEntityExtensionInterface|null
*/
public function getExtensionAttributes(): ?\Smile\CustomEntity\Api\Data\CustomEntityExtensionInterface;
public function getExtensionAttributes(): ?CustomEntityExtensionInterface;

/**
* Set an extension attributes object.
*
* @param \Smile\CustomEntity\Api\Data\CustomEntityExtensionInterface $extensionAttributes Extension attributes.
* @param CustomEntityExtensionInterface $extensionAttributes Extension attributes.
* @return $this
*/
public function setExtensionAttributes(
\Smile\CustomEntity\Api\Data\CustomEntityExtensionInterface $extensionAttributes
): self;
public function setExtensionAttributes(CustomEntityExtensionInterface $extensionAttributes): self;
}
39 changes: 22 additions & 17 deletions Block/Adminhtml/Attribute/Edit/Tab/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

/**
* Custom entity attribute edit main form.
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
class Main extends \Smile\ScopedEav\Block\Adminhtml\Attribute\Edit\Tab\Main
{
protected Options $attributeSetOptions;

public function __construct(
Context $context,
Registry $registry,
Expand All @@ -27,7 +31,7 @@ public function __construct(
YesnoFactory $yesnoFactory,
InputtypeFactory $inputTypeFactory,
PropertyLocker $propertyLocker,
protected Options $attributeSetOptions,
Options $attributeSetOptions,
array $disableScopeChangeList = [],
array $data = []
) {
Expand All @@ -42,6 +46,7 @@ public function __construct(
$disableScopeChangeList,
$data
);
$this->attributeSetOptions = $attributeSetOptions;
}

/**
Expand Down Expand Up @@ -72,24 +77,24 @@ protected function _prepareForm(): self
'frontend_input'
);

//Added dependency between 'Custom entity type' and 'Input Type'.
$this->setChild(
'form_after',
$this->getLayout()->createBlock(
Dependence::class
)->addFieldMap(
"frontend_input",
'frontend_input_type'
)->addFieldMap(
"custom_entity_attribute_set_id",
'custom_entity_attribute_set_id'
)->addFieldDependence(
'custom_entity_attribute_set_id',
'frontend_input_type',
'smile_custom_entity_select'
)
/** @var Dependence $block */
$block = $this->getLayout()->createBlock(Dependence::class);

$block->addFieldMap(
"frontend_input",
'frontend_input_type'
)->addFieldMap(
"custom_entity_attribute_set_id",
'custom_entity_attribute_set_id'
)->addFieldDependence(
'custom_entity_attribute_set_id',
'frontend_input_type',
'smile_custom_entity_select'
);

//Added dependency between 'Custom entity type' and 'Input Type'.
$this->setChild('form_after', $block);

// Disable 'Custom entity type' input if the attribute is already created.
if ($this->getAttributeObject() && $this->getAttributeObject()->getAttributeId()) {
$form->getElement('custom_entity_attribute_set_id')->setDisabled(1);
Expand Down
11 changes: 7 additions & 4 deletions Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Smile\CustomEntity\Controller;

use Exception;
use Magento\Framework\App\Action\Forward;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\RouterInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\UrlInterface;
use Smile\CustomEntity\Model\CustomEntity;
use Smile\CustomEntity\Model\CustomEntity\AttributeSet\Url;

Expand Down Expand Up @@ -56,26 +59,26 @@ public function match(RequestInterface $request)
$requestPathArray = explode('/', $requestPath);
if (
!$this->isValidPath($requestPathArray)
|| $request->getAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS)
|| $request->getAlias(UrlInterface::REWRITE_REQUEST_PATH_ALIAS)
) {
// Continuing with processing of this URL.
return null;
}

try {
$entityId = $this->matchCustomEntity($requestPathArray);
$request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $requestPath)
$request->setAlias(UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $requestPath)
->setModuleName('custom_entity')
->setControllerName($this->getControllerName($requestPathArray))
->setActionName('view')
->setParam('entity_id', $entityId);
} catch (\Exception $e) {
} catch (Exception $e) {
// Continuing with processing of this URL.
return null;
}

return $this->actionFactory->create(
\Magento\Framework\App\Action\Forward::class
Forward::class
);
}

Expand Down
7 changes: 4 additions & 3 deletions Model/CustomEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Smile\CustomEntity\Model;

use Exception;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
Expand Down Expand Up @@ -160,15 +161,15 @@ public function get($entityId, ?int $storeId = null, bool $forceReload = false):
/**
* Delete custom entity.
*
* @param CustomEntityInterface $entity Deleted entity.
* @param CustomEntityInterface|DataObject $entity Deleted entity.
* @return bool Will returned True if deleted
* @throws StateException
*/
public function delete(CustomEntityInterface $entity): bool
public function delete($entity): bool
{
try {
$this->customEntityResource->delete($entity);
} catch (\Exception $e) {
} catch (Exception $e) {
throw new StateException(__('Cannot delete entity with id %1', $entity->getId()), $e);
}

Expand Down
12 changes: 6 additions & 6 deletions Model/ResourceModel/CustomEntity/Attribute/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Smile\CustomEntity\Model\ResourceModel\CustomEntity\Attribute;

use Magento\Eav\Model\ResourceModel\Entity\Attribute;
use Magento\Framework\DB\Ddl\Table;
use Smile\CustomEntity\Model\CustomEntity\Attribute as CustomEntityAttribute;
use Smile\CustomEntity\Model\ResourceModel\CustomEntity\Attribute\Collection as SmileCustomEntityCollection;

/**
Expand Down Expand Up @@ -36,10 +39,7 @@ public function addVisibleFilter(): ?SmileCustomEntityCollection
*/
protected function _construct()
{
$this->_init(
\Smile\CustomEntity\Model\CustomEntity\Attribute::class,
\Magento\Eav\Model\ResourceModel\Entity\Attribute::class
);
$this->_init(CustomEntityAttribute::class, Attribute::class);
}

/**
Expand All @@ -49,7 +49,7 @@ protected function _construct()
protected function _initSelect(): self
{
$entityTypeId = $this->eavConfig->getEntityType(
\Smile\CustomEntity\Model\CustomEntity\Attribute::ENTITY_TYPE_CODE
CustomEntityAttribute::ENTITY_TYPE_CODE
)->getEntityTypeId();

$columns = $this->getConnection()->describeTable($this->getResource()->getMainTable());
Expand All @@ -58,7 +58,7 @@ protected function _initSelect(): self

foreach ($columns as $labelColumn => $columnData) {
$retColumns[$labelColumn] = $labelColumn;
if ($columnData['DATA_TYPE'] == \Magento\Framework\DB\Ddl\Table::TYPE_TEXT) {
if ($columnData['DATA_TYPE'] == Table::TYPE_TEXT) {
$retColumns[$labelColumn] = 'main_table.' . $labelColumn;
}
}
Expand Down
7 changes: 3 additions & 4 deletions Model/ResourceModel/CustomEntity/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Smile\CustomEntity\Model\ResourceModel\CustomEntity;

use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
use Smile\CustomEntity\Model\CustomEntity;
use Smile\CustomEntity\Model\ResourceModel\CustomEntity as CustomEntityResource;

/**
* Custom entity collection model.
Expand Down Expand Up @@ -37,9 +39,6 @@ public function addIsActiveFilter(): self
*/
protected function _construct(): void
{
$this->_init(
\Smile\CustomEntity\Model\CustomEntity::class,
\Smile\CustomEntity\Model\ResourceModel\CustomEntity::class
);
$this->_init(CustomEntity::class, CustomEntityResource::class);
}
}
9 changes: 7 additions & 2 deletions Model/Source/Attribute/CustomEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
*/
class CustomEntity extends AbstractSource
{
protected CustomEntityRepositoryInterface $customEntityRepository;
protected SearchCriteriaBuilder $searchCriteriaBuilder;

public function __construct(
protected CustomEntityRepositoryInterface $customEntityRepository,
protected SearchCriteriaBuilder $searchCriteriaBuilder,
CustomEntityRepositoryInterface $customEntityRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->customEntityRepository = $customEntityRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
Expand Down
Loading

0 comments on commit 1a7f290

Please sign in to comment.