Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't import known types #406

Merged
merged 1 commit into from
Dec 17, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Phpro\SoapClient\CodeGenerator\LaminasCodeFactory\DocBlockGeneratorFactory;
use Phpro\SoapClient\CodeGenerator\Model\ClientMethod;
use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use Phpro\SoapClient\CodeGenerator\Util\TypeChecker;
use Phpro\SoapClient\Exception\AssemblerException;
use Phpro\SoapClient\Exception\SoapException;
use Phpro\SoapClient\Type\MultiArgumentRequest;
Expand Down Expand Up @@ -213,6 +214,9 @@ private function generateSingleArgumentDocblock(ClientMethodContext $context): D
*/
protected function generateClassNameAndAddImport(string $fqcn, ClassGenerator $class, $prefixed = false): string
{
if (TypeChecker::isKnownType($fqcn)) {
return $fqcn;
}
$prefix = '';
$fqcn = ltrim($fqcn, '\\');
$parts = explode('\\', $fqcn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,51 @@ function it_throws_an_exception_when_wrong_context_is_passed() {
);
$clientMethodAssembler->assemble($context);
}

/**
* @test
*/
function it_does_not_import_known_types() {
$assembler = new ClientMethodAssembler();
$class = new ClassGenerator();
$class->setName('Vendor\\MyNamespace\\MyClient');
$typeNamespace = 'Vendor\\MyTypeNamespace';
$method = new ClientMethod(
'Function_name',
[
new Parameter('param', 'string'),
],
'return_type',
$typeNamespace
);

$context = new ClientMethodContext($class, $method);
$assembler->assemble($context);

$code = $context->getClass()->generate();
$expected = <<<CODE
namespace Vendor\MyNamespace;

use Phpro\SoapClient\Type\ResultInterface;
use Vendor\MyTypeNamespace;
use Phpro\SoapClient\Exception\SoapException;
use Phpro\SoapClient\Type\RequestInterface;

class MyClient
{
/**
* @param RequestInterface|string \$param
* @return ResultInterface|MyTypeNamespace\ReturnType
* @throws SoapException
*/
public function function_name(string \$param) : \Vendor\MyTypeNamespace\ReturnType
{
return (\$this->caller)('Function_name', \$param);
}
}

CODE;

$this->assertEquals($expected, $code);
}
}