vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Symbol is declared but not used.intelephense(1003)

Open outcomer opened this issue 4 years ago • 8 comments

Describe the bug The extension does not see the use of a private property

To Reproduce

<?php

class One {

	private $helpers;

	private function __construct() {
		$this->helpers = 10;
	}
}

For me in this code Intelephense show warning on $helpers - Symbol '$helpers' is declared but not used.intelephense(1003) But i use it and code works as expevted. What am i missing? Why $hepleprs become used if it is of type protected?

Expected behavior I expect $helper prop not to be grayed as not in use.

Screenshots image

Platform and version Win 10. Intelephense 1.5.4.

outcomer avatar Nov 20 '20 10:11 outcomer

In the example private $helpers is written to but never read, therefore could safely be removed from the class. When protected it is possible it is used outside of the class.

bmewburn avatar Nov 22 '20 03:11 bmewburn

@bmewburn Ah that is the case! The property is considered as usable if it been read. But in example i write value (10) into it. I would like to discuss a real case and get your opinion. So if i remove like this

class One {
	private function __construct() {
		$this->helpers = 10;
	}
}

then PHP auto declare it by default as public and everybody will get read/write access to it.

What i am doing (tbh this is common pattern - getter/setter):

class One {

        private $helpers;

	private function __construct() {
		$this->helpers = new ClassHelpers();
	}

	public function get_module( $module_name ) {
		return $this->{$module_name};
	}
}

On runtime get_module invokes like this One->get_module('helpers'); So it returns $this->helpers. It could be declared directly like this:

public function get_helpers() {
	return $this->helpers;
}

And prop would be in use, cause i read it. But i have about 150 modules so it is headache to not to forget to write getter for each prop. If this a big problem to recognize that $this->helpers in fact uses via $this->get_module()? Or do you think this code architecture is wrong?

outcomer avatar Nov 22 '20 11:11 outcomer

Thanks for the example. You're right, it shouldn't report this if there is dynamic property access.

bmewburn avatar Nov 24 '20 02:11 bmewburn

@bmewburn Thanks!

outcomer avatar Nov 24 '20 11:11 outcomer

This is very common in SilverStripe as the ORM defines its database fields with private static $db = ...; along with a lot of other private static fields.

https://docs.silverstripe.org/en/4/developer_guides/model/how_tos/dynamic_default_fields/

Would be good to add exceptions to the "unused" rule, as there is usually between 3 and 6 private static fields per data object class implementation.

OldStarchy avatar Jan 25 '21 04:01 OldStarchy

I am having the 1003 issue but with a different aspect: image In this case Intelephense is thinking that Exception is part of the namespace even though it is not. If I switch it to \ then I get this image

rwildcard avatar Mar 03 '22 16:03 rwildcard

i am having the not used.intelephense(1003) also, but i still don't know what the problem in my case image

R1il avatar Jun 04 '22 04:06 R1il

You are using a ‘$email’ instead of a “$email” the first way won’t work and in won’t be detected by your IDE

RYan

On Jun 4, 2022, at 12:50 AM, Syahril Ulya @.***> wrote:



i am having the not used.intelephense(1003) also, but i still don't know what the problem in my case [image]https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F88923674%2F171983625-8f6fd53c-9cd9-4a4f-bf53-20767bb631f8.png&data=05%7C01%7C%7Ca2d7312df55740d3b5b008da45e5c488%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637899150390862626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=28iYnWs2vPPreAeyDHxjs%2FS9wEyH5IE6n8JbdA%2BmpPc%3D&reserved=0

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fbmewburn%2Fvscode-intelephense%2Fissues%2F1519%23issuecomment-1146533536&data=05%7C01%7C%7Ca2d7312df55740d3b5b008da45e5c488%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637899150390862626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=RtA7QUmhz7TlhVMmiLTaDkh2GUI1ucMYhoYLjYLNrGM%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAQNETCQUE4GQ6WELVN55NLVNLOBZANCNFSM4T4SDCNA&data=05%7C01%7C%7Ca2d7312df55740d3b5b008da45e5c488%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637899150390862626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=d7s3KaZ0Odi3R7ZO8w8yEa73OcRQBjnWn35OWUPA4Ic%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

rwildcard avatar Jun 04 '22 14:06 rwildcard