Lines
96.29%
52 / 54
Methods
66.66%
4 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| emitFile | 100.00% | 36 / 36 | 100.00% | 1 / 1 | 3 | ||
| buildEntityIdCompletion | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 3.02 | ||
| buildIdCompletion | 83.33% | 5 / 6 | 0.00% | 0 / 1 | 2.02 | ||
| buildDomainSetName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| buildSetRegistration | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| buildExpectation | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 22 | final readonly class PhpStormMetaEmitter implements ModelFileEmitter | |
| 23 | { | |
| 24 | public const string FILE_NAME = '.phpstorm.meta.php'; | |
| 25 | ||
| 26 | private const string META_NAMESPACE = 'PHPSTORM_META'; | |
| 27 | ||
| 28 | private const string ZONE_DOMAIN = 'zone'; | |
| 29 | ||
| 30 | // forEntities() is variadic, and PhpStorm completes each argument position on its own. | |
| 31 | private const int COMPLETED_VARIADIC_ARGUMENTS = 6; | |
| 32 | ||
| 33 | private const array ENTITY_ID_METHODS = [ | |
| 34 | HaContext::class . '::getState', | |
| 35 | HaContext::class . '::requireState', | |
| 36 | HaContext::class . '::getHeldStateDuration', | |
| 37 | HaContext::class . '::hasHeldStateFor', | |
| 38 | HaContext::class . '::getEntity', | |
| 39 | HaContext::class . '::getHistory', | |
| 40 | HaContext::class . '::waitForState', | |
| 41 | HaContext::class . '::listStates', | |
| 42 | HaContext::class . '::watchStateChanges', | |
| 43 | Scheduler::class . '::runAtEntityTime', | |
| 44 | HaTrigger::class . '::atTime', | |
| 45 | HaTrigger::class . '::onZoneTransition', | |
| 46 | Registry::class . '::findEntity', | |
| 47 | Registry::class . '::findEntityPlacement', | |
| 48 | RegistryEditor::class . '::updateEntity', | |
| 49 | EntityId::class . '::__construct', | |
| 50 | EntityId::class . '::fromStringOrId', | |
| 51 | EntityId::class . '::tryFromString', | |
| 52 | ]; | |
| 53 | ||
| 54 | private const array ACTION_NAME_METHODS = [ | |
| 55 | HaContext::class . '::callAction', | |
| 56 | HaContext::class . '::callActionForResponse', | |
| 57 | ]; | |
| 58 | ||
| 59 | public function emitFile(GenerationModel $model, EmitContext $context): GeneratedFile | |
| 60 | { | |
| 61 | $statements = []; | |
| 62 | ||
| 63 | foreach ($model->listEntityDomains() as $domain) { | |
| 64 | $set = $this->buildDomainSetName($domain); | |
| 65 | $statements[] = $this->buildSetRegistration($set, $domain->entityIds->toStrings()); | |
| 66 | $statements[] = $this->buildExpectation($context->resolveClassName($domain->getEntitiesClass()) . '::getEntity', $set); | |
| 67 | ||
| 68 | if ($domain->domain === self::ZONE_DOMAIN) { | |
| 69 | $statements[] = $this->buildExpectation(HaTrigger::class . '::onZoneTransition', $set, 1); | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | $statements = [ | |
| 74 | ...$statements, | |
| 75 | ...$this->buildEntityIdCompletion($model), | |
| 76 | ...$this->buildIdCompletion('stewart_action_names', $model->listActionNames(), self::ACTION_NAME_METHODS), | |
| 77 | ...$this->buildIdCompletion('stewart_area_ids', $model->areaIds->toStrings(), [ | |
| 78 | EntityFilter::class . '::inArea', | |
| 79 | EntityFilter::class . '::withArea', | |
| 80 | Registry::class . '::findArea', | |
| 81 | Registry::class . '::listDevicesInArea', | |
| 82 | ActionTarget::class . '::forAreas', | |
| 83 | AreaId::class . '::__construct', | |
| 84 | ]), | |
| 85 | ...$this->buildIdCompletion('stewart_floor_ids', $model->floorIds->toStrings(), [ | |
| 86 | EntityFilter::class . '::onFloor', | |
| 87 | EntityFilter::class . '::withFloor', | |
| 88 | Registry::class . '::findFloor', | |
| 89 | Registry::class . '::listAreasOnFloor', | |
| 90 | ActionTarget::class . '::forFloors', | |
| 91 | FloorId::class . '::__construct', | |
| 92 | ]), | |
| 93 | ...$this->buildIdCompletion('stewart_label_ids', $model->labelIds->toStrings(), [ | |
| 94 | EntityFilter::class . '::labelled', | |
| 95 | EntityFilter::class . '::withLabel', | |
| 96 | Registry::class . '::findLabel', | |
| 97 | ActionTarget::class . '::forLabels', | |
| 98 | LabelId::class . '::__construct', | |
| 99 | ]), | |
| 100 | ]; | |
| 101 | ||
| 102 | return $context->printStatements(self::FILE_NAME, self::META_NAMESPACE, implode("\n", $statements)); | |
| 103 | } | |
| 104 | ||
| 105 | /** @return list<string> */ | |
| 106 | private function buildEntityIdCompletion(GenerationModel $model): array | |
| 107 | { | |
| 108 | $entityIds = [...$model->entityIds->toStrings(), ...$model->ignoredEntityIds->toStrings()]; | |
| 109 | sort($entityIds); | |
| 110 | $statements = $this->buildIdCompletion('stewart_entity_ids', $entityIds, self::ENTITY_ID_METHODS); | |
| 111 | ||
| 112 | if ($statements === []) { | |
| 113 | return []; | |
| 114 | } | |
| 115 | ||
| 116 | for ($argument = 0; $argument < self::COMPLETED_VARIADIC_ARGUMENTS; ++$argument) { | |
| 117 | $statements[] = $this->buildExpectation(ActionTarget::class . '::forEntities', 'stewart_entity_ids', $argument); | |
| 118 | } | |
| 119 | ||
| 120 | return $statements; | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * @param list<string> $ids | |
| 125 | * @param list<string> $methods | |
| 126 | * @return list<string> | |
| 127 | */ | |
| 128 | private function buildIdCompletion(string $set, array $ids, array $methods): array | |
| 129 | { | |
| 130 | if ($ids === []) { | |
| 131 | return []; | |
| 132 | } | |
| 133 | ||
| 134 | return [ | |
| 135 | $this->buildSetRegistration($set, $ids), | |
| 136 | ...array_map(fn(string $method): string => $this->buildExpectation($method, $set), $methods), | |
| 137 | ]; | |
| 138 | } | |
| 139 | ||
| 140 | private function buildDomainSetName(DomainModel $domain): string | |
| 141 | { | |
| 142 | return \sprintf('stewart_%s_entity_ids', $domain->domain); | |
| 143 | } | |
| 144 | ||
| 145 | /** @param list<string> $values */ | |
| 146 | private function buildSetRegistration(string $set, array $values): string | |
| 147 | { | |
| 148 | $arguments = array_map(static fn(string $value): string => var_export($value, true), [$set, ...$values]); | |
| 149 | ||
| 150 | return \sprintf('registerArgumentsSet(%s);', implode(', ', $arguments)); | |
| 151 | } | |
| 152 | ||
| 153 | private function buildExpectation(string $method, string $set, int $argument = 0): string | |
| 154 | { | |
| 155 | return \sprintf("expectedArguments(\\%s(), %d, argumentsSet('%s'));", $method, $argument, $set); | |
| 156 | } | |
| 157 | } |