Skip to content

Commit 4154423

Browse files
committed
Add contracts
1 parent 4a42b6c commit 4154423

File tree

10 files changed

+36
-54
lines changed

10 files changed

+36
-54
lines changed

src/Blueprint.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Blueprint;
44

55

6+
use Blueprint\Contracts\Generator;
7+
use Blueprint\Contracts\Lexer;
68
use Symfony\Component\Yaml\Yaml;
79

810
class Blueprint
@@ -38,12 +40,12 @@ public function generate(array $tree)
3840
}
3941
}
4042

41-
public function registerLexer($lexer)
43+
public function registerLexer(Lexer $lexer)
4244
{
4345
$this->lexers[] = $lexer;
4446
}
4547

46-
public function registerGenerator($generator)
48+
public function registerGenerator(Generator $generator)
4749
{
4850
$this->generators[] = $generator;
4951
}

src/Contracts/Generator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Blueprint\Contracts;
4+
5+
interface Generator
6+
{
7+
public function output(array $tree): void;
8+
}

src/Contracts/Lexer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Blueprint\Contracts;
4+
5+
interface Lexer
6+
{
7+
public function analyze(array $tokens): array;
8+
}

src/Generators/FactoryGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Contracts\Generator;
56
use Blueprint\Model;
67

7-
class FactoryGenerator
8+
class FactoryGenerator implements Generator
89
{
9-
public function output(array $tree)
10+
public function output(array $tree): void
1011
{
1112
// TODO: what if changing an existing model
1213
$stub = file_get_contents('stubs/factory.stub');

src/Generators/Generator.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Generators/MigrationGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Contracts\Generator;
56
use Blueprint\Model;
67
use Illuminate\Support\Str;
78

8-
class MigrationGenerator
9+
class MigrationGenerator implements Generator
910
{
10-
public function output(array $tree)
11+
public function output(array $tree): void
1112
{
1213
// TODO: what if changing an existing model
1314
$stub = file_get_contents('stubs/migration.stub');

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Blueprint\Generators;
44

55
use Blueprint\Column;
6+
use Blueprint\Contracts\Generator;
67
use Blueprint\Model;
78

8-
class ModelGenerator
9+
class ModelGenerator implements Generator
910
{
10-
public function output(array $tree)
11+
public function output(array $tree): void
1112
{
1213
// TODO: what if changing an existing model
1314
$stub = file_get_contents('stubs/model/class.stub');

src/Lexers/Lexer.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Lexers/ModelLexer.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

3-
43
namespace Blueprint\Lexers;
54

6-
75
use Blueprint\Column;
8-
use Blueprint\Factories\ModelFactory;
6+
use Blueprint\Contracts\Lexer;
97
use Blueprint\Model;
108

11-
class ModelLexer
9+
class ModelLexer implements Lexer
1210
{
1311
private static $dataTypes = [
1412
'bigIncrements',
@@ -83,7 +81,7 @@ class ModelLexer
8381
'always'
8482
];
8583

86-
public function analyze(array $tokens)
84+
public function analyze(array $tokens): array
8785
{
8886
$registry = [
8987
'models' => []

tests/Feature/BlueprintTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Tests\Feature;
44

55
use Blueprint\Blueprint;
6+
use Blueprint\Contracts\Generator;
7+
use Blueprint\Contracts\Lexer;
68
use Symfony\Component\Yaml\Exception\ParseException;
79
use Tests\TestCase;
810

@@ -151,7 +153,7 @@ public function analyze_return_default_tree_for_empty_tokens()
151153
*/
152154
public function analyze_uses_register_lexers_to_analyze_tokens()
153155
{
154-
$lexer = \Mockery::mock();
156+
$lexer = \Mockery::mock(Lexer::class);
155157
$tokens = ['tokens' => ['are', 'here']];
156158
$lexer->expects('analyze')
157159
->with($tokens)
@@ -171,7 +173,7 @@ public function analyze_uses_register_lexers_to_analyze_tokens()
171173
*/
172174
public function generate_uses_register_generators_to_generate_code()
173175
{
174-
$generator = \Mockery::mock();
176+
$generator = \Mockery::mock(Generator::class);
175177
$tree = ['branch' => ['code', 'attributes']];
176178
$generator->expects('output')
177179
->with($tree);

0 commit comments

Comments
 (0)