Skip to content

Commit

Permalink
Set Up PHP stan (#39)
Browse files Browse the repository at this point in the history
* Set up PHP stan

* Fix PHP Stan

* Apply php-cs-fixer changes

* Fix PHP Stan

* Deleted unintended file

---------

Co-authored-by: aryaantony92 <aryaantony92@users.noreply.github.com>
  • Loading branch information
aryaantony92 and aryaantony92 authored Aug 3, 2023
1 parent 873ef8d commit 4810b54
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/ci/files/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ENV=test
6 changes: 6 additions & 0 deletions .github/ci/scripts/setup-pimcore-environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -eu -o xtrace

#cp -rv .github/ci/files/var .
cp .github/ci/files/.env .
61 changes: 61 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: "Static Analysis"

on:
schedule:
- cron: '0 3 * * 1,3,5'
pull_request:
branches:
- "[0-9]+.[0-9]+"
- "[0-9]+.x"
paths-ignore:
- 'doc/**'
- 'src/Resources/public/**'
push:
branches:
- "[0-9]+.[0-9]+"
- "[0-9]+.x"
- "*_actions"
jobs:
static-analysis-phpstan:
name: "Static Analysis with PHPStan"
runs-on: "ubuntu-20.04"
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
include:
- { php-version: "8.0", dependencies: "lowest", pimcore_version: "", phpstan_args: "", experimental: false }
- { php-version: "8.1", dependencies: "highest", pimcore_version: "", phpstan_args: "", experimental: false}
- { php-version: "8.1", dependencies: "highest", pimcore_version: "11.x-dev", phpstan_args: "", experimental: true}
steps:
- name: "Checkout code"
uses: "actions/checkout@v2"

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

- name: "Setup Pimcore environment"
run: |
.github/ci/scripts/setup-pimcore-environment.sh
- name: "Update Pimcore version"
env:
PIMCORE_VERSION: "${{ matrix.pimcore_version }}"
run: |
if [ ! -z "$PIMCORE_VERSION" ]; then
composer require --no-update pimcore/pimcore:${PIMCORE_VERSION}
fi
- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Run a static analysis with phpstan/phpstan"
if: ${{ matrix.dependencies == 'highest' }}
run: "vendor/bin/phpstan analyse ${{ matrix.phpstan_args }} -c phpstan.neon --memory-limit=-1"

- name: "Run a static analysis with phpstan/phpstan"
if: ${{ matrix.dependencies == 'lowest' }}
run: "vendor/bin/phpstan analyse ${{ matrix.phpstan_args }} -c phpstan-lowest.neon --memory-limit=-1"
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"require": {
"pimcore/pimcore": "^10.6 || ^11.0"
},
"require-dev": {
"phpstan/phpstan": "^1.2"
},
"extra": {
"pimcore": {
"bundles": [
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline-lowest.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Static method Pimcore\\\\Model\\\\Element\\\\Editlock\\:\\:isLocked\\(\\) invoked with 3 parameters, 2 required.$#"
count: 1
path: src/Controller/AdminController.php
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Static method Pimcore\\\\Model\\\\Element\\\\Editlock\\:\\:isLocked\\(\\) invoked with 2 parameters, 3 required.$#"
count: 1
path: src/Controller/AdminController.php
19 changes: 19 additions & 0 deletions phpstan-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
define('PIMCORE_PROJECT_ROOT', __DIR__);
} elseif (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
define('PIMCORE_PROJECT_ROOT', __DIR__ . '/../../..');
} elseif (getenv('PIMCORE_PROJECT_ROOT')) {
define('PIMCORE_PROJECT_ROOT', getenv('PIMCORE_PROJECT_ROOT'));
} else {
throw new \Exception('Unknown configuration! Pimcore project root not found, please set env variable PIMCORE_PROJECT_ROOT.');
}

include PIMCORE_PROJECT_ROOT . '/vendor/autoload.php';
\Pimcore\Bootstrap::setProjectRoot();
\Pimcore\Bootstrap::bootstrap();

if (!defined('PIMCORE_TEST')) {
define('PIMCORE_TEST', true);
}
3 changes: 3 additions & 0 deletions phpstan-lowest.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
includes:
- phpstan.neon
- phpstan-baseline-lowest.neon
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 4
reportUnmatchedIgnoredErrors: false
paths:
- src
bootstrapFiles:
- phpstan-bootstrap.php
excludePaths:
- src/Migrations/*
- src/Tests/*
includes:
- phpstan-baseline.neon
16 changes: 13 additions & 3 deletions src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\Editlock;
use Pimcore\Model\Element\ValidationException;
use Pimcore\Version;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -243,12 +243,22 @@ public function saveAction(Request $request): JsonResponse
{
$objectId = $request->get('id');

if (Editlock::isLocked($objectId, 'object', $request->getSession()->getId())) {
return $this->jsonResponse(['success' => false, 'message' => 'plugin_objectmerger_object_locked']);
/**
* @TODO Remove when removing support for Pimcore 10
*/
if (Version::getMajorVersion() >= 11) {
if (Editlock::isLocked($objectId, 'object', $request->getSession()->getId())) {
return $this->jsonResponse(['success' => false, 'message' => 'plugin_objectmerger_object_locked']);
}
} else {
if (Editlock::isLocked($objectId, 'object')) {
return $this->jsonResponse(['success' => false, 'message' => 'plugin_objectmerger_object_locked']);
}
}

$attributes = json_decode($request->get('attributes'), true);

/** @var \Pimcore\Model\DataObject\Concrete $object */
$object = AbstractObject::getById($objectId);

$preMergeEvent = new GenericEvent($this, [
Expand Down

0 comments on commit 4810b54

Please sign in to comment.