Skip to content

Commit

Permalink
Merge pull request #21 from elecena/phpstan
Browse files Browse the repository at this point in the history
Introduce phpstan as code linter
  • Loading branch information
macbre committed Mar 22, 2021
2 parents 67d8e1b + 72ccab8 commit c37a120
Show file tree
Hide file tree
Showing 14 changed files with 3,940 additions and 992 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: linter
on:
push:
branches: [ master ]
pull_request:

jobs:
lint:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions:
- '7.3'
- '7.4'

steps:
- name: Checkout
uses: actions/checkout@v2

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

- name: Get composer cache directory
id: composer-cache-directory
run: |
composer config cache-dir
echo "::set-output name=dir::$(composer config cache-dir)"
- name: composer cache
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache-directory.outputs.dir }}
key: php-${{ matrix.php-versions }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
php-${{ matrix.php-versions }}-composer
- name: Compose setup
run: composer install --no-interaction

- name: Lint the code
run: composer run lint
2 changes: 1 addition & 1 deletion app/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* You can perform extra actions here. For instance: load different config on your development environment.
*/

require_once 'nano.php';
require_once __DIR__ . '../vendor/autoload.php';

// initialize instance of framework object
Nano::init();
Expand Down
11 changes: 6 additions & 5 deletions classes/NanoBaseTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nano;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Base class for PHPUnit-based unit tests
Expand All @@ -25,7 +26,7 @@ protected function setUp(): void {
* @param callable|null $onQuery optional callback
* @return NanoDatabaseMock
*/
protected function getDatabaseMock(array $result = [], callable $onQuery = null) {
protected function getDatabaseMock(array $result = [], callable $onQuery = null): NanoDatabaseMock {
$mock = new NanoDatabaseMock($this->app);

$mock->setOnQueryCallback($onQuery);
Expand All @@ -38,9 +39,9 @@ protected function getDatabaseMock(array $result = [], callable $onQuery = null)
* Creates a mock of NanoApp with a given method mocked
* @param string $method
* @param mixed $value
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
protected function getNanoAppMock($method, $value) {
protected function getNanoAppMock(string $method, $value): MockObject {
$mock = $this->createMock(\NanoApp::class);
$mock->method($method)->willReturn($value);

Expand All @@ -49,9 +50,9 @@ protected function getNanoAppMock($method, $value) {

/**
* @param NanoObject $obj
* @param \PHPUnit_Framework_MockObject_MockObject $mock
* @param MockObject $mock
*/
protected function setNanoAppMock(NanoObject $obj, \PHPUnit_Framework_MockObject_MockObject $mock) {
protected function setNanoAppMock(NanoObject $obj, MockObject $mock) {
// make sphinx property a public one
$reflection = new \ReflectionClass($obj);
$reflection_property = $reflection->getProperty('app');
Expand Down
18 changes: 14 additions & 4 deletions classes/NanoConsole.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@
* TODO: handle catchable fatals @see http://stackoverflow.com/questions/2468487/how-can-i-catch-a-catchable-fatal-error-on-php-type-hinting
*/
class NanoConsole {

/**
/**
* @var string
*/
private $prompt;
/**
* @var bool
*/
private $useReadline;
/**
* @var string
*/
private $readLineHistoryFileName;

/**
* Detect the environment
*/
function __construct() {
Expand Down Expand Up @@ -39,7 +51,6 @@ function __construct() {
* @return string
*/
public function banner() {
/* @var NanoApp $app */
global $app;

$banner = <<<BANNER
Expand Down Expand Up @@ -69,7 +80,6 @@ public function loop() {
* @return string the result
*/
protected function execute( $line ) {
/* @var NanoApp $app */
global $app;

# add trailing semicolon
Expand Down
9 changes: 7 additions & 2 deletions classes/logger/SimpleSyslogUdpHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nano\Logger;

use DateTimeInterface;
use Monolog\Handler\SyslogUdpHandler;

/**
Expand All @@ -13,8 +14,12 @@ class SimpleSyslogUdpHandler extends SyslogUdpHandler {

/**
* Make common syslog header (see rfc5424)
*/
protected function makeCommonSyslogHeader($severity)
*
* @param int $severity
* @param DateTimeInterface $datetime
* @return string
*/
protected function makeCommonSyslogHeader(int $severity, DateTimeInterface $datetime): string
{
$priority = $severity + $this->facility;

Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
],
"coverage": [
"phpunit --coverage-html=.coverage"
],
"lint": [
"phpstan analyse app/ classes/ tests/"
]
},
"require-dev": {
"nunomaduro/larastan": "^0.7.1"
}
}

0 comments on commit c37a120

Please sign in to comment.