| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AlecRabbit\Accessories\MemoryUsage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AlecRabbit\Formatters\Contracts\FormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use AlecRabbit\Reports\Core\AbstractReport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use AlecRabbit\Reports\Core\AbstractReportable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class MemoryUsageReport extends AbstractReport implements MemoryUsageReportInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $usage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $peakUsage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $usageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $peakUsageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var null|int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $usageDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var null|int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $peakUsageDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var null|int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $usageRealDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var null|int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $peakUsageRealDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var null|MemoryUsageReportFormatter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * MemoryUsageReport constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param int $usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param int $peakUsage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param int $usageReal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param int $peakUsageReal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param FormatterInterface|null $formatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param AbstractReportable|null $reportable | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 8 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         int $usage = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         int $peakUsage = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         int $usageReal = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         int $peakUsageReal = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         FormatterInterface $formatter = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         AbstractReportable $reportable = null | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 8 |  |         parent::__construct($formatter, $reportable); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 8 |  |         $this->usage = $usage ?? memory_get_usage(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 8 |  |         $this->peakUsage = $peakUsage ?? memory_get_peak_usage(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 8 |  |         $this->usageReal = $usageReal ?? memory_get_usage(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 8 |  |         $this->peakUsageReal = $peakUsageReal ?? memory_get_peak_usage(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |     public function getUsage(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         return $this->usage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 6 |  |     public function getPeakUsage(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |         return $this->peakUsage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |     public function getUsageReal(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 6 |  |         return $this->usageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 6 |  |     public function getPeakUsageReal(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 6 |  |         return $this->peakUsageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return null|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 6 |  |     public function getUsageDiff(): ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 6 |  |         return $this->usageDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return null|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |     public function getPeakUsageDiff(): ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  |         return $this->peakUsageDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return null|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |     public function getUsageRealDiff(): ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |         return $this->usageRealDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return null|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |     public function getPeakUsageRealDiff(): ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  |         return $this->peakUsageRealDiff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 2 |  |     public function getUsageString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |         return $this->prepareString($this->usage, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param null|int $forValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param null|string $unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param null|int $decimals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  |     protected function prepareString(?int $forValue, ?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |         if ($this->formatter instanceof MemoryUsageReportFormatter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |             $forValue = $forValue ?? 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |                 $this->formatter->getString($forValue, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         // @codeCoverageIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return 'WRONG FORMATTER TYPE: ' . get_class($this->formatter); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         // @codeCoverageIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 2 |  |     public function getPeakUsageString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 2 |  |         return $this->prepareString($this->peakUsage, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  |     public function getUsageRealString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         return $this->prepareString($this->usageReal, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |     public function getPeakUsageRealString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  |         return $this->prepareString($this->peakUsageReal, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  |     public function getUsageDiffString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 1 |  |         return $this->prepareString($this->usageDiff, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 1 |  |     public function getPeakUsageDiffString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |         return $this->prepareString($this->peakUsageDiff, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  |     public function getUsageRealDiffString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  |         return $this->prepareString($this->usageRealDiff, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 1 |  |     public function getPeakUsageRealDiffString(?string $unit = null, ?int $decimals = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 1 |  |         return $this->prepareString($this->peakUsageRealDiff, $unit, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 3 |  |     public function diff(MemoryUsageReport $firstReport): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 3 |  |         $this->usageDiff = $this->usage - $firstReport->usage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 3 |  |         $this->peakUsageDiff = $this->peakUsage - $firstReport->peakUsage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 3 |  |         $this->usageRealDiff = $this->usageReal - $firstReport->usageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 3 |  |         $this->peakUsageRealDiff = $this->peakUsageReal - $firstReport->peakUsageReal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 217 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 218 |  |  |  |