Completed
Push — master ( 1cd2c7...bcc930 )
by Guillermo
10s
created

EnumPHPDocTester::enumClass()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Atrapalo\PHPTools\Enum;
4
5
use Atrapalo\PHPTools\Parser\PHPDocClass;
6
7
/**
8
 * Class EnumPHPDocTester
9
 * @package Atrapalo\PHPTools\Enum
10
 *
11
 * @author Guillermo González <[email protected]>
12
 */
13
trait EnumPHPDocTester
14
{
15
    /**
16
     * @return string
17
     */
18
    abstract protected function enumClass(): string;
19
20
    /**
21
     * @test
22
     */
23 1
    public function staticAccessByPhpDoc()
24
    {
25 1
        $className = $this->enumClass();
26
27 1
        $staticMethods = PHPDocClass::staticMethods($className);
28 1
        if (!empty($staticMethods)) {
29 1
            foreach ($staticMethods as $staticMethod) {
30 1
                $data = call_user_func([$className, $staticMethod->name()]);
31
32 1
                $this->assertInstanceOf($className, $data);
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() 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...
33
            }
34
        } else {
35
            $this->markTestSkipped('Skipped because no static methods were found for '.get_called_class());
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() 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...
36
        }
37 1
    }
38
39
    /**
40
     * @test
41
     */
42 1
    public function accessByPhpDoc()
43
    {
44 1
        $className = $this->enumClass();
45
46 1
        $methods = PHPDocClass::methods($className);
47 1
        if (!empty($methods)) {
48
49 1
            $enum = $this->createValidEnum($className);
50 1
            $actualMethod = $this->snakeCaseToCamelCase("IS_".$enum->key());
51 1
            foreach ($methods as $method) {
52 1
                $methodName = $method->name();
53 1
                if ($methodName === $actualMethod) {
54 1
                    $this->assertTrue($enum->$methodName());
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
55
                } else {
56 1
                    $this->assertFalse($enum->$methodName());
0 ignored issues
show
Bug introduced by
It seems like assertFalse() 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...
57
                }
58
            }
59
        } else {
60
            $this->markTestSkipped('Skipped because no methods were found for '.get_called_class());
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() 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...
61
        }
62 1
    }
63
64
    /**
65
     * @param Enum|string $className
66
     * @return Enum
67
     */
68 1
    private function createValidEnum($className)
69
    {
70 1
        $values = $className::toArray();
71
72 1
        return new $className($values[array_rand($values)]);
73
    }
74
75
    /**
76
     * @param string $string
77
     * @return string
78
     */
79
    private function snakeCaseToCamelCase(string $string): string
80
    {
81 1
        return preg_replace_callback('/_(.?)/', function($matches) {
82 1
            return ucfirst($matches[1]);
83 1
        }, strtolower($string));
84
    }
85
}