Skip to content

Commit

Permalink
Psalm level 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Dec 28, 2020
1 parent b286122 commit 7a4b8a7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.travis.yml export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
stubs/ export-ignore
tests/ export-ignore

# Other dotfiles
Expand Down
6 changes: 5 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -12,4 +12,8 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<stubs>
<file name="stubs/composer.phpstub" />
</stubs>
</psalm>
4 changes: 2 additions & 2 deletions src/ComposerInstaller/CmsInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public function getInstallPath(PackageInterface $package): string
}

// use path from configuration, otherwise fall back to default
if (isset($extra['kirby-cms-path'])) {
if (isset($extra['kirby-cms-path']) === true) {
$path = $extra['kirby-cms-path'];
} else {
$path = 'kirby';
}

// if explicitly set to something invalid (e.g. `false`), install to vendor dir
if (!is_string($path)) {
if (is_string($path) !== true) {
return parent::getInstallPath($package);
}

Expand Down
5 changes: 3 additions & 2 deletions src/ComposerInstaller/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ protected function postInstall(PackageInterface $package)
{
// remove the package's `vendor` directory to avoid duplicated autoloader and vendor code
$packageVendorDir = $this->getInstallPath($package) . '/vendor';
if (is_dir($packageVendorDir)) {
if (is_dir($packageVendorDir) === true) {
$success = $this->filesystem->removeDirectory($packageVendorDir);
if (!$success) {

if ($success !== true) {
throw new RuntimeException('Could not completely delete ' . $packageVendorDir . ', aborting.'); // @codeCoverageIgnore
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/ComposerInstaller/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\ComposerInstaller;

use Composer\Package\PackageInterface;
use InvalidArgumentException;

/**
* @package Kirby Composer Installer
Expand Down Expand Up @@ -47,12 +48,20 @@ public function getInstallPath(PackageInterface $package): string
// use base path from configuration, otherwise fall back to default
$basePath = $extra['kirby-plugin-path'] ?? 'site/plugins';

if (is_string($basePath) !== true) {
throw new InvalidArgumentException('Invalid "kirby-plugin-path" option');
}

// determine the plugin name from its package name;
// can be overridden in the plugin's `composer.json`
$prettyName = $package->getPrettyName();
$pluginExtra = $package->getExtra();
if (!empty($pluginExtra['installer-name'])) {
if (empty($pluginExtra['installer-name']) === false) {
$name = $pluginExtra['installer-name'];

if (is_string($name) !== true) {
throw new InvalidArgumentException('Invalid "installer-name" option in plugin ' . $prettyName);
}
} elseif (strpos($prettyName, '/') !== false) {
// use name after the slash
$name = explode('/', $prettyName)[1];
Expand Down
35 changes: 35 additions & 0 deletions stubs/composer.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Composer
{
class Config
{
/**
* Returns a setting
*
* @template T as string
* @param T $key
* @param int $flags
* @psalm-return (T is 'vendor-dir' ? string|null : mixed)
*/
public function get($key, $flags = 0)
{
}
}
}

namespace Composer\Installer
{
class LibraryInstaller
{
/**
* @var \Composer\Composer
*/
protected $composer;

/**
* @var \Composer\Util\Filesystem
*/
protected $filesystem;
}
}
29 changes: 29 additions & 0 deletions tests/ComposerInstaller/PluginInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ public function testGetInstallPathCustomPaths()
$this->assertEquals('data/plugins/another-name', $this->installer->getInstallPath($package));
}

public function testGetInstallPathInvalidPluginPath()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Invalid "kirby-plugin-path" option');

$this->initRootPackage()->setExtra([
'kirby-plugin-path' => false
]);

$package = $this->pluginPackageFactory(self::SUPPORTED);
$this->installer->getInstallPath($package);
}

public function testGetInstallPathInvalidInstallerName()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Invalid "installer-name" option in plugin superwoman/superplugin');

$this->initRootPackage()->setExtra([
'kirby-plugin-path' => 'data/plugins'
]);

$package = $this->pluginPackageFactory(self::SUPPORTED);
$package->setExtra([
'installer-name' => true
]);
$this->installer->getInstallPath($package);
}

public function testInstallNoSupport()
{
$package = $this->pluginPackageFactory(self::VENDOR_DIR);
Expand Down

0 comments on commit 7a4b8a7

Please sign in to comment.