Passed
Push — master ( 6f7339...89f27a )
by P.R.
02:14
created

PropertyReadWriteCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\Console\Kernel\Command;
5
6
use Plaisio\Console\Command\PlaisioCommand;
7
use Plaisio\Console\Helper\TwoPhaseWrite;
8
use Plaisio\Console\Kernel\Helper\ClassHelper;
9
use Plaisio\PlaisioKernel;
10
use SetBased\Helper\Cast;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Command for make a property in the kernel read/write (i.e. class \Plaisio\Kernel\Nub).
17
 */
18
class PropertyReadWriteCommand extends PlaisioCommand
19
{
20
  //--------------------------------------------------------------------------------------------------------------------
21
  /**
22
   * @inheritdoc
23
   */
24
  protected function configure()
25
  {
26
    $this->setName('plaisio:kernel-property-read-write')
27
         ->setDescription('Makes a property of the kernel read/write')
28
         ->addArgument('name', InputArgument::REQUIRED, 'The name of the property');
29
  }
30
31
  //--------------------------------------------------------------------------------------------------------------------
32
  /**
33
   * @inheritdoc
34
   */
35
  protected function execute(InputInterface $input, OutputInterface $output): int
36
  {
37
    $this->io->title('Plaisio: Read/Write Kernel Property');
38
39
    $name = Cast::toManString($input->getArgument('name'));
40
41
    $nubPath = ClassHelper::classPath(PlaisioKernel::class);
42
    $source  = $this->readWriteProperty($nubPath, $name);
43
44
    $helper = new TwoPhaseWrite($this->io);
45
    $helper->write($nubPath, $source);
46
47
    return 0;
48
  }
49
50
  //--------------------------------------------------------------------------------------------------------------------
51
  /**
52
   * Adds a property to the source of a class.
53
   *
54
   * @param string $path The path to the source of the class.
55
   * @param string $name The name of the property.
56
   *
57
   * @return string
58
   */
59
  private function readWriteProperty(string $path, string $name): string
60
  {
61
    $name = '$'.ltrim($name, '$');
62
63
    $source = file_get_contents($path);
64
    $lines  = explode(PHP_EOL, $source);
65
66
    $key = ClassHelper::propertyDeclarationLine($lines, $name);
67
68
    $lines[$key] = str_replace('@property-read', '@property     ', $lines[$key]);
69
70
    return implode(PHP_EOL, $lines);
71
  }
72
73
  //--------------------------------------------------------------------------------------------------------------------
74
}
75
76
//----------------------------------------------------------------------------------------------------------------------
77