Skip to content

Commit

Permalink
WIP : PHP8 (#381)
Browse files Browse the repository at this point in the history
* Update libs with wpcs 3 changes [beta] (#373)

* Update composer dependencies

* Update phpcs config

* Remove unused ignored error

* Fix deprecation notice

Depth parameter shouldn't be set to 0.

* Fix the tests failures because of inability to check the list of items vs source items

* Remove extra space from init blocks command

* Update readme

* Fix phpcs violation

* Fix deprecations with using traits directly

Added anonymous classes instead.

* Add mocks for the failing tests

* Fix test deprecations

* Update the gh action workflow

* Workflow fix

* Workflow update

8.3 allow failure for non-released version of PHP.

* Minor formatting fix on helpers

* Replace assertions with expectations API

Should prevent failures on PHPUnit <9 where assertObjectHasProperty isn't defined.

* Prevent the dynamic properties deprecation notices in tests

This will still be an issue on composer installs with lowest dependencies where dynamic properties isn't allowed/fixed in mockery.

* Minor grammar typo fix in a test

* Update workflow to allow failures on lowest composer dependencies

* Update moveItem method

* Improve type description in moveItem method

* Improve the moveItems method

In real life cases, the sourceItem array will contain list of items in the folder, and the folder as a value, so we need to check that as well. This should probably come up in tests, unless the dataset is not correctly set to mimick how the real life projects are behaving. This needs investigation.

* changing the commands registration order

* addings fixes for php8

* Update composer.json

Co-authored-by: Denis Žoljom <[email protected]>

* updating tests

* updating tests

* updating tests

* updating tests

* updating tests

* removing 'Old' function in blocks

* changing config version and name definition

* removing storybook

* fixing missing frontend libs private dir

* fixing wrong block dependeny output

* fixing command examples

* adding new command for changing version name

* fixing import command

* adding better copy for missing attribute

* adding is used method to every enqueu hook

* Update src/Misc/VersionCli.php

Co-authored-by: Ivan Ramljak <[email protected]>

* adding is used method to every enqueu hook

* adding PR fixes

---------

Co-authored-by: Denis Žoljom <[email protected]>
Co-authored-by: Ivan Ramljak <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2023
1 parent d7150e8 commit 6680acf
Show file tree
Hide file tree
Showing 68 changed files with 848 additions and 621 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Check file permissions"
run: |
Expand All @@ -40,7 +40,7 @@ jobs:
coverage: "none"

- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Find non-printable ASCII characters (box-drawing characters excluded)"
run: |
Expand All @@ -63,10 +63,8 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version:
- "7.4"
php-version: ["7.4", "8.0", "8.1", "8.2"]
dependencies:
- "lowest"
- "highest"
runs-on: "ubuntu-latest"
steps:
Expand All @@ -77,14 +75,18 @@ jobs:
coverage: "xdebug"

- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Execute unit tests"
- name: "Execute unit tests without coverage"
run: "composer run test:unit"

- name: "Execute unit tests with coverage"
if: "${{ env.CODECOV_TOKEN && matrix.php-version == '7.4' && matrix.dependencies == 'highest' }}"
run: "composer run test:coverage"

- name: "Upload coverage to Codecov"
Expand Down Expand Up @@ -113,7 +115,7 @@ jobs:
coverage: "none"

- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Validate Composer configuration"
run: "composer validate --strict"
Expand Down Expand Up @@ -142,7 +144,7 @@ jobs:
tools: "cs2pr"

- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Check adherence to EditorConfig"
uses: "greut/eclint-action@v0"
Expand All @@ -166,7 +168,7 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

- name: "Check exported files"
run: |
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@
"source": "https://github.com/infinum/eightshift-libs"
},
"require": {
"php": "^7.4",
"php": "^7.4 || >=8.0",
"ext-dom": "*",
"ext-json": "*",
"ext-libxml": "*",
"php-di/invoker": "^2.3",
"php-di/php-di": "^6.3"
"php-di/invoker": "^2.3.4",
"php-di/php-di": "^7"
},
"require-dev": {
"captainhook/captainhook": "^5.10",
"brain/monkey": "^2.6.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"infinum/eightshift-coding-standards": "^1.6",
"captainhook/captainhook": "^5.10",
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0.0",
"infinum/eightshift-coding-standards": "2.0.0-beta",
"pestphp/pest": "^1.20",
"php-parallel-lint/php-parallel-lint": "^1.3",
"php-stubs/wordpress-stubs": "^5.9",
"php-stubs/wordpress-stubs": "^6.3",
"roave/security-advisories": "dev-master",
"szepeviktor/phpstan-wordpress": "^1.0.3",
"wp-cli/wp-cli": "^2.4",
"szepeviktor/phpstan-wordpress": "^1.3",
"wp-cli/wp-cli": "^2.9",
"yoast/wp-test-utils": "^1.0"
},
"autoload": {
Expand Down
25 changes: 21 additions & 4 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<rule ref="PHPCompatibilityWP"/>

<!-- Support only latest 3 WP versions. -->
<config name="minimum_supported_wp_version" value="5.6"/>
<config name="minimum_supported_wp_version" value="6.0"/>

<exclude-pattern>/src/CompiledContainer\.php</exclude-pattern>

Expand Down Expand Up @@ -55,9 +55,26 @@
<exclude-pattern>*/src/Cli/*</exclude-pattern>
</rule>

<rule ref="WordPress.Security.EscapeOutput.OutputNotEscaped">
<exclude-pattern>*/src/**/*Cli.php</exclude-pattern>
<exclude-pattern>*/src/Cli/*</exclude-pattern>
<!-- Ignore unescaped exceptions. All Eightshift exceptions are escaped. -->
<rule ref="Eightshift.Security.ComponentsEscape.ExceptionNotEscaped">
<severity>0</severity>
</rule>

<!-- Ignore filesystem alternative functions. -->
<rule ref="WordPress.WP.AlternativeFunctions.file_system_operations_mkdir">
<severity>0</severity>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.unlink_unlink">
<severity>0</severity>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.file_system_operations_fopen">
<severity>0</severity>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.file_system_operations_fwrite">
<severity>0</severity>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.file_system_operations_fclose">
<severity>0</severity>
</rule>

<rule ref="Generic.Files.LineLength">
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ parameters:
excludePaths:
- src/**/*Example.php
ignoreErrors:
# Uses func_get_args()
- '/^Function apply_filters(_ref_array)? invoked with [34567] parameters, 2 required\.$/'
# Ignore errors about reflection class variable being undefined. Errors are caught.
- '/^Variable \$reflectionClass might not be defined\.$/'
checkGenericClassInNonGenericObjectType: false
2 changes: 1 addition & 1 deletion src/AdminMenus/AdminReusableBlocksMenuCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getDefaultArgs(): array
'title' => 'Reusable blocks',
'menu_title' => 'Reusable blocks',
'capability' => 'edit_posts',
'menu_icon' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path d="m18.492 2.33 3.179 3.179a2.25 2.25 0 0 1 0 3.182l-2.423 2.422A2.501 2.501 0 0 1 21 13.5v5a2.5 2.5 0 0 1-2.5 2.5h-13A2.5 2.5 0 0 1 3 18.5v-13A2.5 2.5 0 0 1 5.5 3h5a2.5 2.5 0 0 1 2.387 1.754L15.31 2.33a2.25 2.25 0 0 1 3.182 0ZM11 13H5v5.5a.5.5 0 0 0 .5.5H11v-6Zm7.5 0H13v6h5.5a.5.5 0 0 0 .5-.5v-5a.5.5 0 0 0-.5-.5Zm-4.06-2.001L13 9.559v1.44h1.44Zm-3.94-6h-5a.5.5 0 0 0-.5.5V11h6V5.5a.5.5 0 0 0-.5-.5Z" fill="black"/></svg>', // phpcs:ignore
'menu_icon' => 'dashicons-welcome-widgets-menus',
'menu_position' => 4,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/AdminMenus/AdminReusableBlocksMenuExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function () {
$this->getMenuTitle(),
$this->getCapability(),
$this->getMenuSlug(),
'',
'', // @phpstan-ignore-line
$this->getIcon(),
$this->getPosition()
);
Expand Down
4 changes: 2 additions & 2 deletions src/AdminMenus/ReusableBlocksHeaderFooterExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ReusableBlocksHeaderFooterExample extends AbstractAdminMenu
/**
* Admin icon.
*/
public const ADMIN_ICON = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 1A1.5 1.5 0 0 0 0 2.5V4h20V2.5A1.5 1.5 0 0 0 18.5 1h-17ZM20 5H0v10h20V5ZM0 17.5V16h20v1.5a1.5 1.5 0 0 1-1.5 1.5h-17A1.5 1.5 0 0 1 0 17.5ZM19 6H1v8h18V6Z" fill="black"/></svg>'; // phpcs:ignore
public const ADMIN_ICON = 'dashicons-embed-photo';

/**
* Menu position for reusable blocks menu.
Expand Down Expand Up @@ -136,7 +136,7 @@ protected function getMenuSlug(): string
*/
protected function getIcon(): string
{
return 'data:image/svg+xml;base64,' . base64_encode(self::ADMIN_ICON); // phpcs:ignore;
return self::ADMIN_ICON;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyticsGdpr/AnalyticsGdprExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function registerGdprModalSettings(): void
/**
* Prepare the data for usage inside of the GDPR modal template.
*
* @return array<string, array> Prepared array filled with the data from options page.
* @return array<string, array<mixed>> Prepared array filled with the data from options page.
*/
public function prepareGdprModalData(): array
{
Expand Down
59 changes: 0 additions & 59 deletions src/Blocks/AbstractBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,39 +153,6 @@ public function getAllBlocksList($allowedBlockTypes, WP_Block_Editor_Context $bl
return $this->getAllAllowedBlocksList([], $blockEditorContext);
}

/**
* Get all blocks with full block name - legacy.
*
* Used to limit what blocks are going to be used in your project using allowed_block_types filter.
*
* @hook allowed_block_types This is a WP 5 - WP 5.7 compatible hook callback. Will not work with WP 5.8!
*
* @param bool|string[] $allowedBlockTypes Array of block type slugs, or boolean to enable/disable all.
* @param WP_Post $post The post resource data.
*
* @return bool|string[] Boolean if you want to disallow or allow all blocks, or a list of allowed blocks.
*/
public function getAllBlocksListOld($allowedBlockTypes, WP_Post $post)
{
if (\gettype($allowedBlockTypes) === 'boolean') {
return $allowedBlockTypes;
}

$allowedBlockTypes = \array_map(
function ($block) {
return $block['blockFullName'];
},
Components::getBlocks()
);

// Allow reusable block.
$allowedBlockTypes[] = 'eightshift-forms/forms';
$allowedBlockTypes[] = 'core/block';
$allowedBlockTypes[] = 'core/template';

return $allowedBlockTypes;
}

/**
* Method used to register all custom blocks with data fetched from blocks manifest.json.
*
Expand Down Expand Up @@ -274,32 +241,6 @@ public function getCustomCategory(array $categories, WP_Block_Editor_Context $bl
);
}

/**
* Create custom category to assign all custom blocks - legacy.
*
* This category will be shown on all blocks list in "Add Block" button.
*
* @hook block_categories This is a WP 5 - WP 5.7 compatible hook callback. Will not work with WP 5.8!
*
* @param array<int, array<string, string|null>> $categories Array of categories for block types.
* @param WP_Post $post Post being loaded.
*
* @return array<int, array<string, string|null>> Array of categories for block types.
*/
public function getCustomCategoryOld(array $categories, WP_Post $post): array
{
return \array_merge(
$categories,
[
[
'slug' => 'eightshift',
'title' => \esc_html__('Eightshift', 'eightshift-libs'),
'icon' => 'admin-settings',
],
]
);
}

/**
* Locate and return template part with passed attributes for wrapper.
*
Expand Down
70 changes: 47 additions & 23 deletions src/Blocks/AbstractBlocksCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function moveItems(array $args, string $source, string $destination, s
self::cliError(
\sprintf(
// translators: %s will be replaced with type of item, and shorten cli path.
"%s files doesn't exist on this path: `%s`. Please check if you have eightshift-frontend-libs installed.",
"%s file doesn't exist on this path: `%s`. Please check if you have eightshift-frontend-libs installed.",
$type,
$this->getShortenCliPathOutput($source)
)
Expand All @@ -67,32 +67,41 @@ protected function moveItems(array $args, string $source, string $destination, s
$sourceItems = \array_fill_keys(\array_values($sourceItems), $source);
$sourceItemsPrivate = [];

if ($sourcePrivate) {
if (\is_dir($sourcePrivate)) {
$sourceItemsPrivate = \array_diff(\scandir($sourcePrivate) ?: [], ['..', '.']); // phpcs:ignore WordPress.PHP.DisallowShortTernary.Found
$sourceItemsPrivate = \array_fill_keys(\array_values($sourceItemsPrivate), $sourcePrivate);
}

$sourceItems = \array_merge($sourceItems, $sourceItemsPrivate);

if (($isSingleFolder || $isFile) && isset($sourceItems[$name])) {
$sourceItems = [
$name => $sourceItems[$name],
];
}

if (!$sourceItems) {
self::cliError(
\sprintf(
// translators: %s will be replaced with type of item, and shorten cli path.
"%s files doesn't exist on this path: `%s`. Please check if you have eightshift-frontend-libs installed.",
// translators: %1$s will be replaced with type of item, %2$s the type and %3$s and shorten cli path.
'%1$s %2$s doesn\'t exist on this path: `%3$s`. Please check if you have eightshift-frontend-libs installed.',
$type,
$isFile ? 'file' : 'folder',
$this->getShortenCliPathOutput($source)
)
);
}

$itemExists = false;
foreach ($itemsList as $item) {
if (!isset($sourceItems[$item])) {
foreach ($sourceItems as $sourceItem => $sourceFolder) {
if (\strpos($sourceItem, $item) !== false) {
$itemExists = true;
break;
}

// in the case of folders, we should also check the source folders.
if (\strpos($sourceFolder, $item) !== false) {
$itemExists = true;
break;
}
}

if (!$itemExists) {
self::cliError(
\sprintf(
// translators: %s will be replaced with type of item, item name and shorten cli path.
Expand All @@ -104,8 +113,6 @@ protected function moveItems(array $args, string $source, string $destination, s
);
}

$source = $sourceItems[$item];

$fullSource = Components::joinPaths([$source, $item]);
$fullDestination = Components::joinPaths([$destination, $item]);

Expand Down Expand Up @@ -224,17 +231,34 @@ private function outputDependencyItems(string $source, string $type): void

if ($dependencies) {
$componentsCommandName = UseComponentCli::COMMAND_NAME;
$allDependencies = \array_map(static fn ($item) => Components::camelToKebabCase($item), $dependencies);
$allDependencies = \implode(', ', \array_unique(\array_values($allDependencies)));
$blocksCommandName = UseBlockCli::COMMAND_NAME;

$this->cliLogAlert(\implode("\n", [
"This {$type} may need some dependencies to work correctly.",
'',
'To add them to your project, run:',
"%Uwp boilerplate {$this->getCommandParentName()} {$componentsCommandName} --name='{$allDependencies}'%n",
'',
'If a dependency already exists in your project, you can skip it.',
]), 'info', 'Dependencies found');
$outputComand = [];

if ($componentsDependencies) {
$componentsDependenciesAll = \array_map(static fn ($item) => Components::camelToKebabCase($item), $componentsDependencies);
$componentsDependenciesAll = \implode(', ', \array_unique(\array_values($componentsDependenciesAll)));

$outputComand[] = "%Uwp boilerplate {$this->getCommandParentName()} {$componentsCommandName} --name='{$componentsDependenciesAll}'%n";
}

if ($innerBlocksDependency) {
$innerBlocksDependencyAll = \array_map(static fn ($item) => Components::camelToKebabCase($item), $innerBlocksDependency);
$innerBlocksDependencyAll = \implode(', ', \array_unique(\array_values($innerBlocksDependencyAll)));

$outputComand[] = "%Uwp boilerplate {$this->getCommandParentName()} {$blocksCommandName} --name='{$innerBlocksDependencyAll}'%n";
}

if ($outputComand) {
$this->cliLogAlert(\implode("\n", [
"This {$type} may need some dependencies to work correctly.",
'',
'To add them to your project, run:',
...$outputComand,
'',
'If a dependency already exists in your project, you can skip it.',
]), 'info', 'Dependencies found');
}
}
}

Expand Down
Loading

0 comments on commit 6680acf

Please sign in to comment.