1
0
mirror of https://github.com/jupeter/clean-code-php.git synced 2025-09-25 21:49:04 +02:00

move out dependencies

This commit is contained in:
Peter Gribanov
2017-08-31 14:40:42 +03:00
committed by GitHub
parent 3c9845324f
commit a0d6ea9770

View File

@@ -340,7 +340,9 @@ function parseBetterJSAlternative($code)
}
```
**Good:**
**Bad too:**
We have carried out some of the functionality, but the `parseBetterJSAlternative()` function is still very complex and not testable.
```php
function tokenize($code)
@@ -380,6 +382,72 @@ function parseBetterJSAlternative($code)
}
```
**Good:**
The best solution is move out the dependencies of `parseBetterJSAlternative()` function.
```php
class Tokenizer
{
public function tokenize($code)
{
$regexes = [
// ...
];
$statements = split(' ', $code);
$tokens = [];
foreach ($regexes as $regex) {
foreach ($statements as $statement) {
$tokens[] = /* ... */;
}
}
return $tokens;
}
}
```
```php
class Lexer
{
public function lexify($tokens)
{
$ast = [];
foreach ($tokens as $token) {
$ast[] = /* ... */;
}
return $ast;
}
}
```
```php
class BetterJSAlternative
{
private $tokenizer;
private $lexer;
public function __construct(Tokenizer $tokenizer, Lexer $lexer)
{
$this->tokenizer = $tokenizer;
$this->lexer = $lexer;
}
public function parse($code)
{
$tokens = $this->tokenizer->tokenize($code);
$ast = $this->lexer->lexify($tokens);
foreach ($ast as $node) {
// parse...
}
}
}
```
Now we can mock the dependencies and test only the work of method `BetterJSAlternative::parse()`.
**[⬆ back to top](#table-of-contents)**
### Remove duplicate code