Skip to content

Commit

Permalink
fix: add psalm CI and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed Nov 15, 2023
1 parent 1b62558 commit ad6f042
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 10 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This workflow is provided via the organization template repository
#
# https://github.com/nextcloud/.github
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization

name: Static analysis

on:
pull_request:
paths:
- 'lib/**'
- 'templates/**'
- 'tests/**'
- 'vendor/**'
- 'vendor-bin/**'
- composer.lock
push:
branches:
- master
- main
- stable*

concurrency:
group: psalm-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
static-analysis:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions: [ '8.1', '8.2' ]
server-versions: [ 'dev-master' ]
fail-fast: false

name: Nextcloud
steps:
- name: Checkout
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Set up php
uses: shivammathur/setup-php@c5fc0d8281aba02c7fda07d3a70cc5371548067d # v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
ini-file: development
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: |
composer require --with-all-dependencies nextcloud/ocp:${{ matrix.server-versions }}
- name: Run coding standards check
run: composer run psalm
4 changes: 2 additions & 2 deletions lib/Db/QueueMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCP\IDBConnection;

/**
* @template-implements QBMapper<QueueFile>
* @template-extends QBMapper<QueueFile>
*/
class QueueMapper extends QBMapper {
/**
Expand Down Expand Up @@ -106,7 +106,7 @@ public function clearQueue(): void {
/**
* @throws \OCP\DB\Exception
*/
public function count() : int {
public function count() : int|false {
$qb = $this->db->getQueryBuilder();
$result = $qb->select($qb->func()->count('id'))
->from($this->getTableName())
Expand Down
5 changes: 4 additions & 1 deletion lib/Listener/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<Event>
*/
class FileListener implements IEventListener {

public function __construct(
Expand Down Expand Up @@ -151,7 +154,7 @@ public function handle(Event $event): void {
}

public function postDelete(Node $node, bool $recurse = true): void {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
if (!$node instanceof File) {
if (!$recurse) {
return;
}
Expand Down
17 changes: 11 additions & 6 deletions lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use OCP\App\IAppManager;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -108,7 +107,14 @@ public function query(string $userId, string $prompt): array {
if ($exApp === null) {
return ['error' => $this->l10n->t('ExApp class not found, please install the AppAPI app from the Nextcloud AppStore')];
}
return $this->requestToExApp($userId, $exApp, '/query', 'GET', $params);

$response = $this->requestToExApp($userId, $exApp, '/query', 'GET', $params);

if (is_array($response)) {
return $response;
}

return ['error' => $this->l10n->t('Could not parse response from CWYD backend')];
}

private static function getExAppUrl(string $protocol, string $host, int $port): string {
Expand All @@ -119,12 +125,11 @@ private static function getExAppUrl(string $protocol, string $host, int $port):
* Request to ExApp with AppAPI auth headers
*
* @param string|null $userId
* @param ExApp $exApp
* @param \OCA\AppAPI\Db\ExApp $exApp
* @param string $route
* @param string $method
* @param array $params
*
* @return array|IResponse|null
* @return array|resource|string
*/
public function requestToExApp(
?string $userId,
Expand All @@ -133,7 +138,7 @@ public function requestToExApp(
string $method = 'POST',
array $params = [],
?string $contentType = null,
): array | IResponse | null {
): mixed {
try {
$url = self::getExAppUrl(
$exApp->getProtocol(),
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function clearQueue(): void {
/**
* @throws \OCP\DB\Exception
*/
public function count(): int {
public function count(): int|false {
return $this->queueMapper->count();
}
}
3 changes: 3 additions & 0 deletions lib/TextProcessing/CwydProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithUserId;

/**
* @template-implements IProviderWithUserId<CwydTaskType>
*/
class CwydProvider implements IProvider, IProviderWithUserId {

Check failure on line 14 in lib/TextProcessing/CwydProvider.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.1, dev-master)

MissingTemplateParam

lib/TextProcessing/CwydProvider.php:14:31: MissingTemplateParam: OCA\Cwyd\TextProcessing\CwydProvider has missing template params when extending OCP\TextProcessing\IProvider, expecting 1 (see https://psalm.dev/182)

Check failure on line 14 in lib/TextProcessing/CwydProvider.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.2, dev-master)

MissingTemplateParam

lib/TextProcessing/CwydProvider.php:14:31: MissingTemplateParam: OCA\Cwyd\TextProcessing\CwydProvider has missing template params when extending OCP\TextProcessing\IProvider, expecting 1 (see https://psalm.dev/182)

private ?string $userId = null;
Expand Down
40 changes: 40 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="tests/psalm-baseline.xml"
>
<!--
SPDX-FileCopyrightText: Marcel Klehr <mklehr@gmx.net>
SPDX-License-Identifier: CC0-1.0
-->
<stubs>
<file name="tests/stub.phpstub" preloadClasses="true"/>
</stubs>
<projectFiles>
<directory name="lib" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<extraFiles>
<directory name="vendor" />
<ignoreFiles>
<directory name="vendor/psalm" />
</ignoreFiles>
</extraFiles>
<issueHandlers>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<referencedClass name="OC\AppFramework\OCS\BaseResponse"/>
<referencedClass name="Doctrine\DBAL\Schema\Schema" />
<referencedClass name="Doctrine\DBAL\Schema\SchemaException" />
<referencedClass name="Doctrine\DBAL\Driver\Statement" />
<referencedClass name="Doctrine\DBAL\Schema\Table" />
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
</psalm>
24 changes: 24 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="lib/AppInfo/Application.php">
<InvalidArgument>
<code>CwydTextProcessingProvider::class</code>
</InvalidArgument>
</file>
<file src="lib/BackgroundJobs/IndexerJob.php">
<UndefinedClass>
<code>\OC_Util</code>
</UndefinedClass>
</file>
<file src="lib/Command/ScanFiles.php">
<UndefinedClass>
<code>Command</code>
</UndefinedClass>
</file>
<file src="lib/Service/LangRopeService.php">
<UndefinedClass>
<code>\OCA\AppAPI\Db\ExApp</code>
<code>\OCA\AppAPI\Service\AppAPIService</code>
</UndefinedClass>
</file>
</files>
142 changes: 142 additions & 0 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);
namespace OC\Hooks {
class Emitter {
public function emit(string $class, string $value, array $option) {}
/** Closure $closure */
public function listen(string $class, string $value, $closure) {}
}
}

namespace OC {
class SystemConfig {
public function getKeys();
public function setValue($key, $value);
public function setValues(array $configs);
public function getValue($key, $default = '');
public function getFilteredValue($key, $default = '');
public function deleteValue($key);
}
}


namespace OC\Files\Cache {
class CacheQueryBuilder extends \OCP\DB\QueryBuilder\IQueryBuilder {
public function __construct(\OCP\IDBCOnnection $db, \OC\SystemConfig $config, \Psr\Log\LoggerInterface $logger);
public function selectFileCache(string $alias = null, bool $joinExtendedCache = true):CacheQueryBuilder;
public function whereStorageId(int $storageId):CacheQueryBuilder;
public function whereFileId(int $fileId):CacheQueryBuilder;
public function wherePath(string $path):CacheQueryBuilder;
public function whereParent(int $parent):CacheQueryBuilder;
public function whereParentInParameter(string $parameter):CacheQueryBuilder;
}
}

namespace Doctrine\DBAL {
final class ParameterType
{
/**
* Represents the SQL NULL data type.
*/
public const NULL = 0;

/**
* Represents the SQL INTEGER data type.
*/
public const INTEGER = 1;

/**
* Represents the SQL CHAR, VARCHAR, or other string data type.
*
* @see \PDO::PARAM_STR
*/
public const STRING = 2;

/**
* Represents the SQL large object data type.
*/
public const LARGE_OBJECT = 3;

/**
* Represents a boolean data type.
*
* @see \PDO::PARAM_BOOL
*/
public const BOOLEAN = 5;

/**
* Represents a binary string data type.
*/
public const BINARY = 16;

/**
* Represents an ASCII string data type
*/
public const ASCII = 17;

/**
* This class cannot be instantiated.
*
* @codeCoverageIgnore
*/
private function __construct()
{
}
}

final class ArrayParameterType
{
/**
* Represents an array of ints to be expanded by Doctrine SQL parsing.
*/
public const INTEGER = ParameterType::INTEGER + Connection::ARRAY_PARAM_OFFSET;

/**
* Represents an array of strings to be expanded by Doctrine SQL parsing.
*/
public const STRING = ParameterType::STRING + Connection::ARRAY_PARAM_OFFSET;

/**
* Represents an array of ascii strings to be expanded by Doctrine SQL parsing.
*/
public const ASCII = ParameterType::ASCII + Connection::ARRAY_PARAM_OFFSET;

/**
* Represents an array of ascii strings to be expanded by Doctrine SQL parsing.
*/
public const BINARY = ParameterType::BINARY + Connection::ARRAY_PARAM_OFFSET;

/**
* @internal
*
* @psalm-param self::* $type
*
* @psalm-return ParameterType::INTEGER|ParameterType::STRING|ParameterType::ASCII|ParameterType::BINARY
*/
public static function toElementParameterType(int $type): int
{
}

private function __construct()
{
}
}

class Connection {
/**
* Represents an array of ints to be expanded by Doctrine SQL parsing.
*/
public const PARAM_INT_ARRAY = ParameterType::INTEGER + self::ARRAY_PARAM_OFFSET;

/**
* Represents an array of strings to be expanded by Doctrine SQL parsing.
*/
public const PARAM_STR_ARRAY = ParameterType::STRING + self::ARRAY_PARAM_OFFSET;

/**
* Offset by which PARAM_* constants are detected as arrays of the param type.
*/
public const ARRAY_PARAM_OFFSET = 100;
}
}

0 comments on commit ad6f042

Please sign in to comment.