1
/**
Annotation ?????
*/ 

public function getProduct($id=null,$params=null) {

if (!empty($params['sku'])  {  
$sql.= " AND sku=:sku " ;
$sql_tab['sku']=$params['sku']; }
if (!empty($params['ean'])  {  
$sql.= " AND ean=:ean " ;
$sql_tab['ean']=$params['ean']; }

 // ..... 
}

Is there a way for my IDE (VS Code / IntelliJ) to help it about autocompletion, based on this quick example ?

I mean, what annotations should I have to for an array ($params here)?

Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
David N
  • 118
  • 1
  • 7
  • Does the PHP version you are using support type hinting on variables themselves? – Nigel Ren May 14 '21 at 10:01
  • There's multiple ways to annotate arrays. Some [examples](https://scrutinizer-ci.com/docs/tools/php/php-analyzer/guides/annotating_code#type-reference) can be found here. You could add something like `@param string[] $params` for an array containing a list of strings. – PtrTon May 14 '21 at 10:06
  • I dont know what you are talking. 7.4 – David N May 14 '21 at 10:06
  • in 7.4 yuo can use something as `public function getProduct(int $id=null, array $params=null) : bool {` – splash58 May 14 '21 at 10:33
  • @splash58 "array" has been available as a type hint since something like 5.0; "int" has been available since 7.0, and frankly nobody should be using a version older than that any more. However, I _suspect_ the OP is asking for something more specific, although the question's not particularly clear. – IMSoP May 14 '21 at 11:54
  • @IMSoP I can be wrong, but, seems, ide's understand annotation in php code. But I have a practice only with phpstorm although ) – splash58 May 14 '21 at 13:25
  • @splash58 Yes, but marking something as an "array" is quite broad. I suspect the OP is hoping for some way of saying "array which must have a key 'sku' with a value which is an int". That said, the best fix is to change the code to expect an object of a particular class, and define the property types on the class. – IMSoP May 14 '21 at 13:28
  • I am asking, with this code (and no one other), to help the ide autocomplete. Not even "integer". Just while I type $class->getProduct(null,.... array( give me hints)) – David N May 14 '21 at 14:18
  • So, you want it to auto-complete the keys, like 'sku', 'ean', etc? – IMSoP May 14 '21 at 14:42

0 Answers0