Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog

## master

BREAKING

- VIM plugin: Local `.phpactor.json` configuration files are no longer loaded by
default. If you use the VIM plugin you **must** explicitly trust the
configuration file with `:PhpactorTrust`.

Improvements:

- Improved inlay type hints #2825 @dantleech
Expand Down
8 changes: 8 additions & 0 deletions autoload/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ function! phpactor#MoveFile()
call phpactor#rpc("move_class", { "source_path": phpactor#_path() })
endfunction

function! phpactor#Trust()
call phpactor#rpc("trust", { "trust": 1 })
endfunction

function! phpactor#Untrust()
call phpactor#rpc("trust", { "trust": 0 })
endfunction

function! phpactor#OffsetTypeInfo()
call phpactor#rpc("offset_info", { "offset": phpactor#_offset(), "source": phpactor#_source()})
endfunction
Expand Down
3 changes: 3 additions & 0 deletions doc/phpactor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ COMMANDS *phpactor-commands*
:[N]PhpactorTransform *:PhpactorTransform*
Automatically add any missing properties to a class

:[N]PhpactorTrust *:PhpactorTrust*
Trust configuration in the current working directory

:PhpactorUpdate *:PhpactorUpdate*
Update Phpactor to the latest version using the branch defined with
|g:phpactorBranch|
Expand Down
17 changes: 17 additions & 0 deletions doc/reference/rpc_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ _RpcHandler_status
**Default**: ``"formatted"``


.. _RpcHandler_trust:


_RpcHandler_trust
-----------------


.. _RpcCommand_trust_trust:


``trust``
"""""""""


**Default**: ``null``


.. _RpcHandler_file_info:


Expand Down
6 changes: 6 additions & 0 deletions ftplugin/php/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ command! -buffer -nargs=0 PhpactorGenerateMutators call phpactor#GenerateMutator
" Automatically add any missing properties to a class
command! -buffer -nargs=0 PhpactorTransform call phpactor#Transform()

""
" Trust configuration in the current working directory
command! -buffer -nargs=0 PhpactorTrust call phpactor#Trust()

" Revoke trust in the current working directory
command! -buffer -nargs=0 PhpactorUntrust call phpactor#Untrust()
8 changes: 8 additions & 0 deletions lib/Extension/Core/Application/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composer\InstalledVersions;
use Phpactor\ConfigLoader\Core\PathCandidates;
use Phpactor\Extension\Core\Trust\Trust;
use Phpactor\Extension\Php\Model\PhpVersionResolver;
use Phpactor\Extension\SourceCodeFilesystem\SourceCodeFilesystemExtension;
use Phpactor\Filesystem\Domain\FilesystemRegistry;
Expand All @@ -21,6 +22,7 @@ public function __construct(
private PathCandidates $paths,
private string $workingDirectory,
private PhpVersionResolver $phpVersionResolver,
private Trust $trust,
?ExecutableFinder $executableFinder = null,
) {
$this->executableFinder = $executableFinder ?: new ExecutableFinder();
Expand Down Expand Up @@ -56,6 +58,12 @@ public function check(): array
$diagnostics['good'][] = 'XDebug is disabled. XDebug has a negative effect on performance.';
}

if ($this->trust->isTrusted($this->workingDirectory)) {
$diagnostics['good'][] = sprintf('Path "%s" is trusted and configuration will be loaded from it.', $this->workingDirectory);
} else {
$diagnostics['bad'][] = sprintf('Path "%s" is not trusted and configuration will not be loaded from it.', $this->workingDirectory);
}

foreach ($this->paths as $configFile) {
$diagnostics['config_files'][$configFile->path()] = file_exists($configFile->path());
}
Expand Down
15 changes: 13 additions & 2 deletions lib/Extension/Core/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phpactor\Extension\Core\Rpc\CacheClearHandler;
use Phpactor\Extension\Core\Rpc\ConfigHandler;
use Phpactor\Extension\Core\Rpc\StatusHandler;
use Phpactor\Extension\Core\Rpc\TrustHandler;
use Phpactor\Extension\Core\Trust\Trust;
use Phpactor\Extension\Php\Model\PhpVersionResolver;
use Phpactor\Extension\Rpc\RpcExtension;
Expand Down Expand Up @@ -110,7 +111,7 @@ private function registerConsole(ContainerBuilder $container): void

$container->register('command.status', function (Container $container) {
return new StatusCommand(
$container->get('application.status')
$container->get('application.status'),
);
}, [ ConsoleExtension::TAG_COMMAND => [ 'name' => 'status' ]]);

Expand Down Expand Up @@ -162,7 +163,9 @@ private function registerApplicationServices(ContainerBuilder $container): void
$container->has('config_loader.candidates') ? $container->get('config_loader.candidates') : new PathCandidates([]),
$container->get(FilePathResolverExtension::SERVICE_FILE_PATH_RESOLVER)->resolve('%project_root%'),
$container->get(PhpVersionResolver::class),
null,
/** @phpstan-ignore argument.type */
$container->parameter(self::PARAM_TRUST)->value(),
null
);
});
}
Expand All @@ -180,6 +183,14 @@ private function registerRpc(ContainerBuilder $container): void
$container->register('core.rpc.handler.config', function (Container $container) {
return new ConfigHandler($container->getParameters());
}, [ RpcExtension::TAG_RPC_HANDLER => ['name' => ConfigHandler::CONFIG] ]);

$container->register('core.rpc.handler.trust', function (Container $container) {
return new TrustHandler(
/** @phpstan-ignore argument.type */
$container->parameter(self::PARAM_TRUST)->value(),
$container->parameter(FilePathResolverExtension::PARAM_PROJECT_ROOT)->string(),
);
}, [ RpcExtension::TAG_RPC_HANDLER => ['name' => TrustHandler::NAME] ]);
}

private function registerFilePathExpanders(ContainerBuilder $container): void
Expand Down
46 changes: 46 additions & 0 deletions lib/Extension/Core/Rpc/TrustHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Phpactor\Extension\Core\Rpc;

use Phpactor\Extension\Core\Trust\Trust;
use Phpactor\Extension\Rpc\Handler;
use Phpactor\Extension\Rpc\Response;
use Phpactor\Extension\Rpc\Response\EchoResponse;
use Phpactor\MapResolver\Resolver;

final class TrustHandler implements Handler
{
const NAME = 'trust';
const PARAM_TRUST = 'trust';

public function __construct(private Trust $status, private string $projectDir)
{
}

public function configure(Resolver $resolver): void
{
$resolver->setRequired([
self::PARAM_TRUST
]);
}

/**
* @param array{trust:int} $arguments
*/
public function handle(array $arguments): Response
{
$trust = (bool)$arguments['trust'];
$this->status->setTrusted($this->projectDir, $trust);

if ($trust) {
return EchoResponse::fromMessage(sprintf('Project directory "%s" is trusted. Configuration will be loaded from it.', $this->projectDir));
}
return EchoResponse::fromMessage(sprintf('Project directory "%s" is not trusted. Configuration will not be loaded from it.', $this->projectDir));

}

public function name(): string
{
return self::NAME;
}
}
12 changes: 7 additions & 5 deletions lib/Phpactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@ public static function boot(InputInterface $input, OutputInterface $output, stri
if (!$trusted) {
foreach ($projectCandidates as [$candidate, $_]) {
if (file_exists($candidate)) {
$errorOutput->writeln(sprintf(
'<fg=yellow>Local config "%s" found but it\'s in an untrusted directory, ' .
'run `phpactor config:trust` if you want it to be loaded</>',
basename($candidate),
));
if ($commandName !== 'rpc') {
$errorOutput->writeln(sprintf(
'<fg=yellow>Local config "%s" found but it\'s in an untrusted directory, ' .
'run `phpactor config:trust` if you want it to be loaded</>',
basename($candidate),
));
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion tests/Unit/Extension/Core/Application/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Phpactor\ConfigLoader\Core\PathCandidates;
use Phpactor\Extension\Core\Trust\Trust;
use Phpactor\Extension\Php\Model\PhpVersionResolver;
use Phpactor\Extension\SourceCodeFilesystem\SourceCodeFilesystemExtension;
use Phpactor\Filesystem\Domain\FilesystemRegistry;
Expand All @@ -30,7 +31,13 @@ public function setUp(): void
$this->registry = $this->prophesize(FilesystemRegistry::class);
$this->resolver = $this->prophesize(PhpVersionResolver::class);
$this->paths = new PathCandidates([]);
$this->status = new Status($this->registry->reveal(), $this->paths, '/path/to/here', $this->resolver->reveal());
$this->status = new Status(
$this->registry->reveal(),
$this->paths,
'/path/to/here',
$this->resolver->reveal(),
new Trust([], null),
);
}

public function testStatusNoComposerOrGit(): void
Expand Down