These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Support\Traits; |
||
4 | |||
5 | use PragmaRX\Health\Data\Models\HealthCheck; |
||
6 | |||
7 | trait Database |
||
8 | { |
||
9 | protected $database; |
||
10 | |||
11 | public function loadDatabase() |
||
12 | { |
||
13 | $this->database = $this->__load(); |
||
14 | } |
||
15 | |||
16 | /** |
||
17 | * Load cache. |
||
18 | * |
||
19 | * @return \Illuminate\Support\Collection |
||
20 | */ |
||
21 | View Code Duplication | public function __load() |
|
0 ignored issues
–
show
|
|||
22 | { |
||
23 | if (!file_exists($file = $this->getDatabaseFileName())) { |
||
24 | return collect(); |
||
25 | } |
||
26 | |||
27 | return collect(json_decode(file_get_contents($file), true)); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get cache filename. |
||
32 | * |
||
33 | * @return string|null |
||
34 | */ |
||
35 | protected function getDatabaseFileName() |
||
36 | { |
||
37 | return $this->target->saveTo; |
||
0 ignored issues
–
show
The property
target does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Check if database is enabled. |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | protected function databaseEnabled() |
||
46 | { |
||
47 | return config('health.database.enabled'); |
||
48 | } |
||
49 | |||
50 | protected function saveResultsToDatabase($target, $result) |
||
51 | { |
||
52 | HealthCheck::create([ |
||
0 ignored issues
–
show
The method
create() does not exist on PragmaRX\Health\Data\Models\HealthCheck . Did you maybe mean created() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
53 | 'resource_name' => $resource = $target->resource->name, |
||
54 | 'resource_slug' => $target->resource->slug, |
||
55 | 'target_name' => $target->name, |
||
56 | 'target_slug' => str_slug($target->name), |
||
57 | 'target_display' => $target->display, |
||
58 | 'healthy' => $result->healthy, |
||
59 | 'error_message' => $result->errorMessage, |
||
60 | 'runtime' => $result->elapsedTime, |
||
61 | 'value' => $result->value, |
||
62 | 'value_human' => $result->valueHuman, |
||
63 | ]); |
||
64 | |||
65 | return |
||
66 | HealthCheck::where([ |
||
67 | 'resource_slug' => $target->resource->slug, |
||
68 | 'target_name' => $target->name, |
||
69 | ]) |
||
70 | ->orderBy('created_at', 'desc') |
||
71 | ->take(config('health.database.max_records')) |
||
72 | ->get(); |
||
73 | } |
||
74 | } |
||
75 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.