Passed
Pull Request — master (#351)
by Brian
456:50 queued 353:15
created

CakePHPInstaller::inflectPackageVars()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
namespace Composer\Installers;
3
4
use Composer\DependencyResolver\Pool;
5
6
class CakePHPInstaller extends BaseInstaller
7
{
8
    protected $locations = array(
9
        'plugin' => 'Plugin/{$name}/',
10
    );
11
12
    /**
13
     * Format package name to CamelCase
14
     */
15
    public function inflectPackageVars($vars)
16
    {
17
        if ($this->matchesCakeVersion('>=', '3.0.0')) {
18
            return $vars;
19
        }
20
21
        $nameParts = explode('/', $vars['name']);
22
        foreach ($nameParts as &$value) {
23
            $value = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $value));
24
            $value = str_replace(array('-', '_'), ' ', $value);
25
            $value = str_replace(' ', '', ucwords($value));
26
        }
27
        $vars['name'] = implode('/', $nameParts);
28
29
        return $vars;
30
    }
31
32
    /**
33
     * Change the default plugin location when cakephp >= 3.0
34
     */
35
    public function getLocations()
36
    {
37
        if ($this->matchesCakeVersion('>=', '3.0.0')) {
38
            $this->locations['plugin'] =  $this->composer->getConfig()->get('vendor-dir') . '/{$vendor}/{$name}/';
0 ignored issues
show
Bug introduced by
The method getConfig() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            $this->locations['plugin'] =  $this->composer->/** @scrutinizer ignore-call */ getConfig()->get('vendor-dir') . '/{$vendor}/{$name}/';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        }
40
        return $this->locations;
41
    }
42
43
    /**
44
     * Check if CakePHP version matches against a version
45
     *
46
     * @param string $matcher
47
     * @param string $version
48
     * @return bool
49
     */
50
    protected function matchesCakeVersion($matcher, $version)
51
    {
52
        if (class_exists('Composer\Semver\Constraint\MultiConstraint')) {
53
            $multiClass = 'Composer\Semver\Constraint\MultiConstraint';
0 ignored issues
show
Unused Code introduced by
The assignment to $multiClass is dead and can be removed.
Loading history...
54
            $constraintClass = 'Composer\Semver\Constraint\Constraint';
55
        } else {
56
            $multiClass = 'Composer\Package\LinkConstraint\MultiConstraint';
57
            $constraintClass = 'Composer\Package\LinkConstraint\VersionConstraint';
58
        }
59
60
        $repositoryManager = $this->composer->getRepositoryManager();
61
        if (! $repositoryManager) {
62
            return false;
63
        }
64
65
        $repos = $repositoryManager->getLocalRepository();
66
        if (!$repos) {
67
            return false;
68
        }
69
70
        return $repos->findPackage('cakephp/cakephp', new $constraintClass($matcher, $version)) !== null;
71
    }
72
}
73