Skip to content

Commit

Permalink
Merge pull request #24 from the-coding-owl/phpstan
Browse files Browse the repository at this point in the history
[TASK] Add phpstan as static code analyser for PHP code
  • Loading branch information
the-coding-owl committed Apr 8, 2020
2 parents 40b2ff2 + 047750d commit 0e7626d
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/actions/install-typo3-cms-dashboard/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM composer:latest

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
6 changes: 6 additions & 0 deletions .github/actions/install-typo3-cms-dashboard/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'Install TYPO3 Dashboard extension via composer'
description: 'Install TYPO3 Dashboard extension via composer'
author: 'Kevin Ditscheid <kevin@the-coding-owl.de>'
runs:
using: docker
image: Dockerfile
3 changes: 3 additions & 0 deletions .github/actions/install-typo3-cms-dashboard/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
exec composer require typo3/cms-dashboard:^10.3.0
4 changes: 4 additions & 0 deletions .github/actions/run-phpstan/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM php:7.3-cli

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
6 changes: 6 additions & 0 deletions .github/actions/run-phpstan/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'Run the phpstan analyser'
description: 'Run the phpstan analyser'
author: 'Kevin Ditscheid <kevin@the-coding-owl.de>'
runs:
using: docker
image: Dockerfile
3 changes: 3 additions & 0 deletions .github/actions/run-phpstan/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
exec php ./vendor/bin/phpstan analyse
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Cache node modules
uses: actions/cache@v1
env:
cache-name: cache-node-modules
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: ESLint Action
uses: stefanoeb/eslint-action@1.0.2

Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: phpstan

on:
pull_request:
branches: [ master ]

jobs:
analyse:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Cache composer dependencies
uses: actions/cache@v1
env:
cache-name: cache-composer-dependencies
with:
path: ./.composer/cache
key: ${{ runner.os }}-build-${{ env.cache-name }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}
${{ runner.os }}-build-
${{ runner.os }}-
- name: Composer install
uses: php-actions/composer@v1

- name: Composer require typo3/cms-dashboard
uses: ./.github/actions/install-typo3-cms-dashboard

- name: Run phpstan
uses: ./.github/actions/run-phpstan
22 changes: 15 additions & 7 deletions Classes/Toolbar/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3Fluid\Fluid\View\ViewInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;

/**
* Clock toolbar class
*/
class Clock implements ToolbarItemInterface {
/**
* @var ViewInterface
* @var StandaloneView
*/
protected $view;

/**
* @var PageRenderer
*/
protected $pageRenderer;

/**
* Constructs the Clock toolbar item
*/
public function __construct() {
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Luxon');
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Clock');
/** @var PageRenderer */
$this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Luxon');
$this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Clock');
/** @var StandaloneView */
$this->view = GeneralUtility::makeInstance(StandaloneView::class);
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('oclock');
/** @var ExtensionConfiguration $extensionConfiguration */
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
$extConf = $extensionConfiguration->get('oclock');
$rootPaths = [
'template' => [
'EXT:oclock/Resources/Private/Templates/'
Expand Down Expand Up @@ -94,7 +102,7 @@ public function getDropDown(): string {
/**
* Get an array with additional attributes for the ToolbarItem container
*
* @return array
* @return string[]
*/
public function getAdditionalAttributes(): array {
$currentDateTime = new \DateTime();
Expand Down
78 changes: 66 additions & 12 deletions Classes/Widgets/ClockWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Dashboard\Widgets\Interfaces\RequireJsModuleInterface;
use TYPO3\CMS\Dashboard\Widgets\Interfaces\AdditionalCssInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* The widget for a clock
*/
class ClockWidget extends AbstractWidget {
class ClockWidget extends AbstractWidget implements RequireJsModuleInterface, AdditionalCssInterface {
/**
* The title of the widget
*
Expand Down Expand Up @@ -51,13 +54,45 @@ class ClockWidget extends AbstractWidget {
* @var int
*/
protected $height = 1;

/**
* The view object
*
* @var StandaloneView
*/
protected $view;

/**
* The extension configuration array
*
* @var array{dashboard:string[],additionalTemplateRootPath:string,additionalPartialRootPath:string,additionalLayoutRootPath:string}
*/
protected $extConf = [
'dashboard' => [
'css' => 'EXT:oclock/Resources/Public/Stylesheets/dashboard.css'
],
'additionalTemplateRootPath' => '',
'additionalPartialRootPath' => '',
'additionalLayoutRootPath' => ''
];

/**
* Constructor of the ClockWidget
*
* @param string $identifier
*/
public function __construct(string $identifier) {
parent::__construct($identifier);
/** @var ExtensionConfiguration $extensionConfiguration */
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->extConf = $extensionConfiguration->get('oclock');
}

/**
* Initialize the widget view
*/
protected function initializeView(): void {
parent::initializeView();
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('oclock');
$rootPaths = [
'template' => [
'EXT:oclock/Resources/Private/Templates/'
Expand All @@ -69,29 +104,48 @@ protected function initializeView(): void {
'EXT:oclock/Resources/Private/Layout/'
]
];
if(!empty($extConf['additionalTemplateRootPath'])) {
$templateRootPaths['template'][] = $extConf['additionalTemplateRootPath'];
if(!empty($this->extConf['additionalTemplateRootPath'])) {
$templateRootPaths['template'][] = $this->extConf['additionalTemplateRootPath'];
}
if(!empty($extConf['additionalPartialRootPath'])) {
$templateRootPaths['partial'][] = $extConf['additionalPartialRootPath'];
if(!empty($this->extConf['additionalPartialRootPath'])) {
$templateRootPaths['partial'][] = $this->extConf['additionalPartialRootPath'];
}
if(!empty($extConf['additionalLayoutRootPath'])) {
$templateRootPaths['layout'][] = $extConf['additionalLayoutRootPath'];
if(!empty($this->extConf['additionalLayoutRootPath'])) {
$templateRootPaths['layout'][] = $this->extConf['additionalLayoutRootPath'];
}
$this->view->setTemplateRootPaths($rootPaths['template']);
$this->view->setPartialRootPaths($rootPaths['partial']);
$this->view->setLayoutRootPaths($rootPaths['layout']);
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Luxon');
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Oclock/Clock');
$pageRenderer->addCssFile($extConf['dashboard']['css']);
}

/**
* Render the widget
*
* @return string
*/
public function renderWidgetContent(): string {
$this->view->assign('date', new \DateTime());
return $this->view->render();
}

/**
* Get the CSS file array
*
* @return string[]
*/
public function getCssFiles(): array {
return [$this->extConf['dashboard']['css']];
}

/**
* Get the requireJS modules
*
* @return string[]
*/
public function getRequireJsModules(): array {
return [
'TYPO3/CMS/Oclock/Luxon',
'TYPO3/CMS/Oclock/Clock',
];
}
}
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@
},
"replace": {
"typo3-ter/oclock": "self.version"
},
"require-dev": {
"phpstan/phpstan": "^0.12.18",
"saschaegerer/phpstan-typo3": "^0.13.1",
"phpstan/extension-installer": "^1.0"
}
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: max
paths:
- Classes
ignoreErrors:
-
message: '#Property TheCodingOwl\\Oclock\\Widgets\\ClockWidget::$extConf \(array\<string, array\<string\>|string\>\) does not accept mixed\.#'
path: Classes/Widgets/ClockWidget.php

0 comments on commit 0e7626d

Please sign in to comment.