From 911ec037c692ce00b621f79fe8222ab35beab6ca Mon Sep 17 00:00:00 2001 From: korelstar Date: Thu, 13 Feb 2020 12:44:56 +0100 Subject: [PATCH 1/4] use xmllint-problem-matcher --- .github/workflows/lint.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1fdc79201..4deb7ab22 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,6 +33,8 @@ jobs: uses: actions/checkout@master - name: Setup xmllint run: sudo apt-get install --no-install-recommends -y libxml2-utils + - name: Setup xmllint problem matcher + uses: korelstar/xmllint-problem-matcher@master - name: lint XML run: make lint-xml From 4fc8e1a9888e8ff73de2c9e5713c8b0f012c3556 Mon Sep 17 00:00:00 2001 From: korelstar Date: Thu, 13 Feb 2020 15:22:52 +0100 Subject: [PATCH 2/4] add version matrix for lint-php --- .github/workflows/lint.yml | 15 ++++++- tests/nextcloud-version.php | 78 +++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 21 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4deb7ab22..467dd81a9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -40,12 +40,25 @@ jobs: lint-php: runs-on: ubuntu-latest + strategy: + matrix: + version: [min, max] + include: + - version: min + php-version: 7.0 + - version: max + php-version: 7.4 steps: - name: Checkout uses: actions/checkout@master - name: Set up php${{ matrix.php-versions }} - uses: shivammathur/setup-php@master + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php-version }} - name: Install Dependencies run: composer install --prefer-dist + - name: Install latest Nextcloud API (dev-master) + if: matrix.version == 'max' + run: composer require --dev christophwurst/nextcloud:dev-master - name: lint PHP run: make lint-php diff --git a/tests/nextcloud-version.php b/tests/nextcloud-version.php index 0ec1afcb7..b85eb3575 100644 --- a/tests/nextcloud-version.php +++ b/tests/nextcloud-version.php @@ -12,24 +12,45 @@ function getNCVersionFromComposer($path) { if(!is_object($json)) { throw new Exception('Composer file does not contain valid JSON'); } - if(!property_exists($json, 'require-dev')) { - throw new Exception('Composer file has no "require-dev" section'); + $dev = getValidProperty($json, 'require-dev'); + $v = getValidProperty($dev, 'christophwurst/nextcloud'); + if(substr($v, 0, 1)=='^') { + $v = substr($v, 1); } - $dev = $json->{'require-dev'}; - if(!is_object($dev)) { - throw new Exception('Composer file has no valid "require-dev" section'); + return $v; +} + + +function getNCVersionFromComposerBranchAlias($path) { + if(!file_exists($path)) { + throw new Exception('Composer file does not exists: '.$path); } - if(!property_exists($dev, 'christophwurst/nextcloud')) { - throw new Exception('Composer file has no "nextcloud" dependency'); + if(!is_readable($path)) { + throw new Exception('Composer file is not readable: '.$path); } - $v = $dev->{'christophwurst/nextcloud'}; - if(substr($v, 0, 1)=='^') { - $v = substr($v, 1); + $content = file_get_contents($path); + $json = json_decode($content); + if(!is_object($json)) { + throw new Exception('Composer file does not contain valid JSON'); + } + $extra = getValidProperty($json, 'extra'); + $branchAlias = getValidProperty($extra, 'branch-alias'); + $v = getValidProperty($branchAlias, 'dev-master'); + if(substr($v, -4)=='-dev') { + $v = substr($v, 0, -4); } return $v; } -function getNCVersionFromAppInfo($path) { + +function getValidProperty($json, $prop) { + if(!property_exists($json, $prop)) { + throw new Exception('Composer file has no "'.$prop.'" section'); + } + return $json->{$prop}; +} + +function getNCVersionFromAppInfo($path, $minmax='min') { if(!file_exists($path)) { throw new Exception('AppInfo does not exists: '.$path); } @@ -39,21 +60,38 @@ function getNCVersionFromAppInfo($path) { $content = file_get_contents($path); $info = new SimpleXMLElement($content); $nc = $info->dependencies->nextcloud; - $v = (string)$nc->attributes()->{'min-version'}; - if(strpos($v, '.') === false) { - $v .= '.0'; - } + $v = (string)$nc->attributes()->{$minmax.'-version'}; return $v; } -echo 'Testing Nextcloud min-version: '; +function versionCompare($sv1, $sv2) { + $v1 = explode('.', $sv1); + $v2 = explode('.', $sv2); + $count = min(count($v1), count($v2)); + for($i=0; $i<$count; $i++) { + if($v1[$i] !== $v2[$i]) { + return false; + } + } + return true; +} + +echo 'Testing Nextcloud version '; try { $vComposer = getNCVersionFromComposer(__DIR__.'/../composer.json'); - $vAppInfo = getNCVersionFromAppInfo(__DIR__.'/../appinfo/info.xml'); - if($vComposer === $vAppInfo) { - echo $vAppInfo.PHP_EOL; + if($vComposer === 'dev-master') { + $vComposer = getNCVersionFromComposerBranchAlias(__DIR__.'/../vendor/christophwurst/nextcloud/composer.json'); + $vAppInfo = getNCVersionFromAppInfo(__DIR__.'/../appinfo/info.xml', 'max'); + echo 'max'; + } else { + $vAppInfo = getNCVersionFromAppInfo(__DIR__.'/../appinfo/info.xml', 'min'); + echo 'min'; + } + echo ': '.$vAppInfo.' (AppInfo) vs. '.$vComposer.' (Composer) => '; + if(versionCompare($vComposer, $vAppInfo)) { + echo 'OK'.PHP_EOL; } else { - echo 'FAILED with '.$vComposer.' (Composer) vs. '.$vAppInfo.' (AppInfo)'.PHP_EOL; + echo 'FAILED'.PHP_EOL; exit(1); } } catch(Exception $e) { From 7b7060814e1430c53ca0cc802664514ce0e3a251 Mon Sep 17 00:00:00 2001 From: korelstar Date: Thu, 13 Feb 2020 16:39:05 +0100 Subject: [PATCH 3/4] add phplint-problem-matcher --- .github/workflows/lint.yml | 6 ++++-- .phan/to-annotation.php | 21 --------------------- Makefile | 23 ++++++++++++++++++----- composer.json | 3 ++- 4 files changed, 24 insertions(+), 29 deletions(-) delete mode 100644 .phan/to-annotation.php diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 467dd81a9..c39896aa0 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -45,7 +45,7 @@ jobs: version: [min, max] include: - version: min - php-version: 7.0 + php-version: 7.1 - version: max php-version: 7.4 steps: @@ -60,5 +60,7 @@ jobs: - name: Install latest Nextcloud API (dev-master) if: matrix.version == 'max' run: composer require --dev christophwurst/nextcloud:dev-master + - name: Load problem matcher for php -l + uses: korelstar/phplint-problem-matcher@master - name: lint PHP - run: make lint-php + run: make -k lint-php diff --git a/.phan/to-annotation.php b/.phan/to-annotation.php deleted file mode 100644 index d20dbdf73..000000000 --- a/.phan/to-annotation.php +++ /dev/null @@ -1,21 +0,0 @@ -severity >= 10 ? 'error' : 'warning'); - echo '::' . $type; - echo ' file=' . $item->location->path; - echo ',line=' . $item->location->lines->begin; - $message = explode(' ', $item->description, 3); - echo '::' . $message[2] . ' ['.$message[1].']'; - echo PHP_EOL; - if ($type === 'error') { - $errors++; - } -} - -exit($errors); - diff --git a/Makefile b/Makefile index a4f3ee799..80369acd6 100644 --- a/Makefile +++ b/Makefile @@ -90,19 +90,31 @@ test-watch: test-coverage: npm run test:coverage + # Linting lint: lint-php lint-js lint-css lint-xml -lint-php: + +lint-php: lint-php-lint lint-php-ncversion lint-php-phan lint-php-phpcs +lint-phpfast: lint-php-lint lint-php-ncversion lint-php-phpcs + +lint-php-lint: # Check PHP syntax errors @! find lib/ -name "*.php" | xargs -I{} php -l '{}' | grep -v "No syntax errors detected" - # PHP CodeSniffer - vendor/bin/phpcs --standard=phpcs.xml --runtime-set ignore_warnings_on_exit 1 appinfo/ lib/ - # PHAN - vendor/phan/phan/phan --allow-polyfill-parser -k .phan/config.php --no-progress-bar -m json | php .phan/to-annotation.php + +lint-php-ncversion: # Check min-version consistency php tests/nextcloud-version.php +lint-php-phan: + # PHAN + vendor/bin/phan --allow-polyfill-parser -k .phan/config.php --no-progress-bar -m checkstyle | vendor/bin/cs2pr + +lint-php-phpcs: + # PHP CodeSniffer + vendor/bin/phpcs --standard=phpcs.xml appinfo/ lib/ --report=checkstyle | vendor/bin/cs2pr + + lint-js: npm run lint @@ -114,6 +126,7 @@ lint-xml: wget https://apps.nextcloud.com/schema/apps/info.xsd -P appinfo/ -N --no-verbose || [ -f appinfo/info.xsd ] xmllint appinfo/info.xml --schema appinfo/info.xsd --noout + # Fix lint lint-fix: lint-php-fix lint-js-fix lint-css-fix diff --git a/composer.json b/composer.json index 18c1adbc3..fb0ddf9e8 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "require-dev": { "christophwurst/nextcloud": "^15.0", "squizlabs/php_codesniffer": "3.*", - "phan/phan": "^2.0" + "phan/phan": "^2.0", + "staabm/annotate-pull-request-from-checkstyle": "^1.0" } } From aaf1a2fc46724ae0d7527da3e09a67be02a23886 Mon Sep 17 00:00:00 2001 From: korelstar Date: Thu, 13 Feb 2020 21:48:46 +0100 Subject: [PATCH 4/4] fix code style --- lib/Service/NoteUtil.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Service/NoteUtil.php b/lib/Service/NoteUtil.php index aee94f5ab..8985da3a7 100644 --- a/lib/Service/NoteUtil.php +++ b/lib/Service/NoteUtil.php @@ -208,8 +208,8 @@ public function getOrCreateFolder(string $path) : Folder { /* * Delete a folder and it's parent(s) if it's/they're empty - * @param Folder root folder for notes - * @param Folder folder to delete + * @param Folder $notesFolder root folder for notes + * @param Folder $folder folder to delete */ public function deleteEmptyFolder(Folder $notesFolder, Folder $folder) : void { $content = $folder->getDirectoryListing(); @@ -225,13 +225,19 @@ public function deleteEmptyFolder(Folder $notesFolder, Folder $folder) : void { /** * Checks if there is enough space left on storage. Throws an Exception if storage is not sufficient. - * @param Folder folder that needs storage + * @param Folder $folder that needs storage + * @param int $requiredBytes amount of storage needed in $folder * @throws InsufficientStorageException */ public function ensureSufficientStorage(Folder $folder, $requiredBytes) : void { $availableBytes = $folder->getFreeSpace(); if ($availableBytes >= 0 && $availableBytes < $requiredBytes) { - $this->logger->error('Insufficient storage in '.$folder->getPath().': available are '.$availableBytes.'; required are '.$requiredBytes, ['app' => $this->appName]); + $this->logger->error( + 'Insufficient storage in '.$folder->getPath().': '. + 'available are '.$availableBytes.'; '. + 'required are '.$requiredBytes, + ['app' => $this->appName] + ); throw new InsufficientStorageException($requiredBytes.' are required in '.$folder->getPath()); } }