Skip to content

Commit 9c6d474

Browse files
committed
Refactor FormulaEngine properties to use nullable typed properties and add __clone method to support cloning of dependent objects.
1 parent 2b599c1 commit 9c6d474

1 file changed

Lines changed: 22 additions & 32 deletions

File tree

src/formula/FormulaEngine.php

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,28 @@ class FormulaEngine implements FormulaEngineInterface
3030
{
3131
public const FORMULAS_SEPARATOR = "\n";
3232

33-
/**
34-
* @var Ruler
35-
*/
36-
protected $ruler;
33+
protected ?Ruler $ruler = null;
3734

38-
/**
39-
* @var Visit|Asserter
40-
*/
41-
protected $asserter;
35+
protected ?Visit $asserter = null;
4236

43-
/**
44-
* @var Context
45-
*/
46-
protected $context;
37+
protected ?Context $context = null;
4738

48-
/**
49-
* @var ChargeModifier
50-
*/
51-
protected $discount;
39+
protected ?ChargeModifier $discount = null;
5240

53-
/**
54-
* @var ChargeModifier
55-
*/
56-
protected $installment;
41+
protected ?ChargeModifier $installment = null;
5742

58-
/**
59-
* @var ChargeModifier
60-
*/
61-
protected $increase;
43+
protected ?ChargeModifier $increase = null;
6244

6345
protected ?Cap $cap = null;
6446

6547
protected ?Once $once = null;
66-
/**
67-
* @var CacheInterface
68-
*/
69-
private $cache;
7048

71-
public function __construct(CacheInterface $cache)
72-
{
49+
public function __construct(
50+
private CacheInterface $cache
51+
) {
7352
if (!class_exists(Context::class)) {
7453
throw new Exception('to use formula engine install `hoa/ruler`');
7554
}
76-
77-
$this->cache = $cache;
7855
}
7956

8057
public function build(string $formula): ChargeModifier
@@ -254,4 +231,17 @@ private function getOnce(): ChargeModifier
254231

255232
return $this->once;
256233
}
234+
235+
public function __clone(): void
236+
{
237+
if ($this->context !== null) {
238+
$this->context = clone $this->context;
239+
}
240+
if ($this->ruler !== null) {
241+
$this->ruler = clone $this->ruler;
242+
}
243+
if ($this->asserter !== null) {
244+
$this->asserter = clone $this->asserter;
245+
}
246+
}
257247
}

0 commit comments

Comments
 (0)