Completed
Push — master ( 150f22...447137 )
by Andrii
02:13
created

RegistryTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 2
cbo 0
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchPackageData() 0 7 1
A buildVcsRepository() 0 7 1
A getPackageSearchUrl() 0 4 1
A buildPackageUrl() 0 4 1
1
<?php
2
3
/*
4
 * Asset Packagist
5
 *
6
 * @link      https://github.com/hiqdev/asset-packagist
7
 * @package   asset-packagist
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\assetpackagist\registry;
13
14
use Composer\Repository\RepositoryManager;
15
16
/**
17
 * Registry trait.
18
 * All this to use protected functions from fxp.
19
 * @property RepositoryManager $rm
20
 */
21
trait RegistryTrait
22
{
23
    public function fetchPackageData($name)
24
    {
25
        $packageUrl = str_replace('%package%', $name, $this->lazyProvidersUrl);
0 ignored issues
show
Bug introduced by
The property lazyProvidersUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $cacheName = $name . '-' . sha1($name) . '-package.json';
27
28
        return $this->fetchFile($packageUrl, $cacheName);
0 ignored issues
show
Bug introduced by
It seems like fetchFile() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
    }
30
31
    /**
32
     * @param $name
33
     * @return \Composer\Repository\RepositoryInterface
34
     */
35
    public function buildVcsRepository($name)
36
    {
37
        $data = $this->fetchPackageData($name);
38
        $conf = $this->createVcsRepositoryConfig($data, $name);
0 ignored issues
show
Bug introduced by
It seems like createVcsRepositoryConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
40
        return $this->rm->createRepository($conf['type'], $conf);
41
    }
42
43
    public function getPackageSearchUrl($name)
44
    {
45
        return $this->siteUrl . '/search/?q=' . $name;
0 ignored issues
show
Bug introduced by
The property siteUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
    }
47
48
    public function buildPackageUrl($name)
49
    {
50
        return $this->getPackageSearchUrl($name);
51
    }
52
}
53