Failed Conditions
Pull Request — master (#15)
by Billie
04:23 queued 01:20
created

src/SwaggerApiInstaller.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PurpleBooth\JaneOpenApiAutogenerate;
4
5
use Composer\Composer;
6
use Composer\Installer\BinaryInstaller;
7
use Composer\Installer\LibraryInstaller;
8
use Composer\IO\IOInterface;
9
use Composer\Package\PackageInterface;
10
use Composer\Util\Filesystem;
11
use Joli\Jane\OpenApi\JaneOpenApi;
12
13
class SwaggerApiInstaller extends LibraryInstaller
14
{
15
    const PACKAGE_TYPE = 'swagger-api';
16
17
    const EXTRA_KEY_NAMESPACE = 'namespace';
18
19
    const EXTRA_KEY_SCHEMA_FILE = 'schema-file';
20
21
    const EXTRA_KEY_ENVIRONMENT_VARIABLE = 'environment-variable';
22
    const GENERATED_DIRECTORY            = "generated";
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function __construct(
28
        IOInterface $inputOutput,
29
        Composer $composer,
30
        $type = self::PACKAGE_TYPE,
31
        Filesystem $filesystem = null,
32
        BinaryInstaller $binaryInstaller = null
33
    ) {
34
        parent::__construct($inputOutput, $composer, $type, $filesystem, $binaryInstaller);
35
    }
36
37
38
    /**
39
     * { @inheritdoc }
40
     */
41
    protected function updateCode(PackageInterface $initial, PackageInterface $target)
42
    {
43
        $this->removeCode($initial);
44
        $this->installCode($target);
45
    }
46
47
    /**
48
     * { @inheritdoc }
49
     */
50
    protected function installCode(PackageInterface $package)
51
    {
52
        parent::installCode($package);
53
54
        $downloadPath = $this->getInstallPath($package);
55
        $this->generateSwaggerClient(
56
            $package,
57
            $downloadPath
58
        );
59
    }
60
61
    /**
62
     * Generate the client for a package
63
     *
64
     * @param PackageInterface $package
65
     * @param string           $downloadPath
66
     */
67
    protected function generateSwaggerClient(PackageInterface $package, $downloadPath)
68
    {
69
        $janeOpenApi = JaneOpenApi::build();
70
71
        $extra             = $package->getExtra();
72
        $namespace         = $extra[ self::EXTRA_KEY_NAMESPACE ];
73
        $openApiSchemaFile = $extra[ self::EXTRA_KEY_SCHEMA_FILE ];
74
75
        if (isset($extra[ self::EXTRA_KEY_ENVIRONMENT_VARIABLE ])) {
76
            $envVariableName = $extra[ self::EXTRA_KEY_ENVIRONMENT_VARIABLE ];
77
            $envVariable     = getenv($envVariableName);
78
79
            if ($envVariable) {
80
                $openApiSchemaFile = $envVariable;
81
            }
82
        }
83
84
        $vendorSchemaPath = implode(DIRECTORY_SEPARATOR, [$downloadPath, $openApiSchemaFile]);
85
86
        if (file_exists($vendorSchemaPath)) {
87
            $openApiSchemaFile = $vendorSchemaPath;
88
        }
89
90
        $this->io->write(
91
            "Generating <info>$namespace</info> from <info>$openApiSchemaFile</info>",
92
            true,
93
            IOInterface::VERBOSE
94
        );
95
96
        $this->io->write(
97
            "Writing to <info>$downloadPath</info>",
98
            true,
99
            IOInterface::VERY_VERBOSE
100
        );
101
102
        $generatePath = $vendorSchemaPath = implode(DIRECTORY_SEPARATOR, [$downloadPath, self::GENERATED_DIRECTORY]);
0 ignored issues
show
$vendorSchemaPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
        $files        = $janeOpenApi->generate($openApiSchemaFile, $namespace, $generatePath);
104
        $janeOpenApi->printFiles($files, $downloadPath);
105
106
        foreach ($files as $file) {
107
            $this->io->write("Generated <info>{$file->getFilename()}</info>", true, IOInterface::DEBUG);
108
        }
109
110
        $this->io->write(
111
            "Generated <info>$namespace</info> from <info>$openApiSchemaFile</info>",
112
            true,
113
            IOInterface::VERBOSE
114
        );
115
    }
116
}
117