From 347c29256f595e49cc72cd283e5e2cb24dd96d3e Mon Sep 17 00:00:00 2001 From: vldf Date: Tue, 29 Jul 2025 15:29:35 +0300 Subject: [PATCH 1/6] add stub tests --- .../kphptags/psi/KphpDocTagStubImpl.kt | 2 + .../fixtures/kphp_stub/kphp-json.fixture.php | 6 ++ .../fixtures/kphp_stub/kphp-json.stub.php | 4 ++ .../kphp_stub/kphp-simple-tags.fixture.php | 9 +++ .../kphp_stub/kphp-simple-tags.stub.php | 10 ++++ .../kphp_stub/kphp-template.fixture.php | 5 ++ .../fixtures/kphp_stub/kphp-template.stub.php | 5 ++ .../kphp-warn-performance.fixture.php | 4 ++ .../kphp_stub/kphp-warn-performance.stub.php | 4 ++ .../testing/infrastructure/StubTestBase.kt | 60 +++++++++++++++++++ .../kphpstorm/testing/tests/KphpStubTest.kt | 21 +++++++ 11 files changed, 130 insertions(+) create mode 100644 src/test/fixtures/kphp_stub/kphp-json.fixture.php create mode 100644 src/test/fixtures/kphp_stub/kphp-json.stub.php create mode 100644 src/test/fixtures/kphp_stub/kphp-simple-tags.fixture.php create mode 100644 src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php create mode 100644 src/test/fixtures/kphp_stub/kphp-template.fixture.php create mode 100644 src/test/fixtures/kphp_stub/kphp-template.stub.php create mode 100644 src/test/fixtures/kphp_stub/kphp-warn-performance.fixture.php create mode 100644 src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php create mode 100644 src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt create mode 100644 src/test/kotlin/com/vk/kphpstorm/testing/tests/KphpStubTest.kt diff --git a/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt b/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt index a47c0b0f..ba652115 100644 --- a/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt +++ b/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt @@ -22,4 +22,6 @@ class KphpDocTagStubImpl( override fun getType() = PhpType() override fun getName() = name override fun getValue() = value + + override fun toString(): String = "$name${if (value != null) ": $value" else ""}" } diff --git a/src/test/fixtures/kphp_stub/kphp-json.fixture.php b/src/test/fixtures/kphp_stub/kphp-json.fixture.php new file mode 100644 index 00000000..0089e9e6 --- /dev/null +++ b/src/test/fixtures/kphp_stub/kphp-json.fixture.php @@ -0,0 +1,6 @@ + Date: Tue, 29 Jul 2025 19:28:04 +0300 Subject: [PATCH 2/6] add todo --- .../kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt b/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt index ba652115..16d3b093 100644 --- a/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt +++ b/src/main/kotlin/com/vk/kphpstorm/kphptags/psi/KphpDocTagStubImpl.kt @@ -11,6 +11,8 @@ import com.jetbrains.php.lang.psi.resolve.types.PhpType * When @kphp-... tags (regardless of elementType) are stored as stubs, * they hold only name and optional arbitrary string value. * Similar to [com.jetbrains.php.lang.documentation.phpdoc.psi.stubs.PhpDocTagStubImpl] + * + * TODO: make a research to check are we really need stubs? */ class KphpDocTagStubImpl( parent: StubElement<*>?, From 9a9f1515478c971c3d1b0bc591db3063ae9e4755 Mon Sep 17 00:00:00 2001 From: vldf Date: Wed, 30 Jul 2025 14:07:57 +0300 Subject: [PATCH 3/6] update kphp-template test --- src/test/fixtures/kphp_stub/kphp-template.fixture.php | 4 ++-- src/test/fixtures/kphp_stub/kphp-template.stub.php | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/fixtures/kphp_stub/kphp-template.fixture.php b/src/test/fixtures/kphp_stub/kphp-template.fixture.php index 58a2574f..f0f62611 100644 --- a/src/test/fixtures/kphp_stub/kphp-template.fixture.php +++ b/src/test/fixtures/kphp_stub/kphp-template.fixture.php @@ -1,5 +1,5 @@ Date: Wed, 30 Jul 2025 16:40:00 +0300 Subject: [PATCH 4/6] use `KphpStormTestBase` as a base class for `StubTestBase` --- .../testing/infrastructure/StubTestBase.kt | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt b/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt index 069ccf18..767e8856 100644 --- a/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt +++ b/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt @@ -1,42 +1,20 @@ package com.vk.kphpstorm.testing.infrastructure -import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess import com.intellij.psi.impl.DebugUtil -import com.intellij.testFramework.fixtures.BasePlatformTestCase -import com.jetbrains.php.config.PhpLanguageLevel -import com.jetbrains.php.config.PhpProjectConfigurationFacade import com.jetbrains.php.lang.psi.stubs.PhpFileStubBuilder import com.vk.kphpstorm.configuration.KphpStormConfiguration import java.io.File -import java.nio.file.Paths -abstract class StubTestBase : BasePlatformTestCase() { - - open val languageLevel: PhpLanguageLevel = PhpLanguageLevel.PHP740 - - override fun getTestDataPath() = "src/test/fixtures" +abstract class StubTestBase : KphpStormTestBase() { private val stubBuilder = PhpFileStubBuilder() - - override fun setUp() { - super.setUp() - VfsRootAccess.allowRootAccess(testRootDisposable, Paths.get(testDataPath).toAbsolutePath().toString()) - } - - private fun setupLanguageLevel() { - val projectConfigurationFacade = PhpProjectConfigurationFacade.getInstance(project) - projectConfigurationFacade.languageLevel = languageLevel - } - /** * Run stub test on the file.fixture.php * file.stub.php must exist. It contains dumped stub tree */ protected fun doStubTest(fixtureFile: String) { - setupLanguageLevel() - KphpStormConfiguration.saveThatSetupForProjectDone(project) myFixture.configureByFile(fixtureFile) From cbc498995e8be0da699a13a4a082c3b2e8a0c7fb Mon Sep 17 00:00:00 2001 From: vldf Date: Wed, 30 Jul 2025 16:32:15 +0300 Subject: [PATCH 5/6] Add missing tests --- .../fixtures/kphp_stub/kphp-json.fixture.php | 8 ++++++- .../fixtures/kphp_stub/kphp-json.stub.php | 5 +++++ .../kphp_stub/kphp-serialize.fixture.php | 22 +++++++++++++++++++ .../kphp_stub/kphp-serialize.stub.php | 17 ++++++++++++++ .../kphpstorm/testing/tests/KphpStubTest.kt | 4 ++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/test/fixtures/kphp_stub/kphp-serialize.fixture.php create mode 100644 src/test/fixtures/kphp_stub/kphp-serialize.stub.php diff --git a/src/test/fixtures/kphp_stub/kphp-json.fixture.php b/src/test/fixtures/kphp_stub/kphp-json.fixture.php index 0089e9e6..c965a018 100644 --- a/src/test/fixtures/kphp_stub/kphp-json.fixture.php +++ b/src/test/fixtures/kphp_stub/kphp-json.fixture.php @@ -3,4 +3,10 @@ /** * @kphp-json rename_policy=camelCase */ -class C { } +class C { + /** + * @kphp-json float_precision=1 + * @kphp-json required + */ + public float $field; +} diff --git a/src/test/fixtures/kphp_stub/kphp-json.stub.php b/src/test/fixtures/kphp_stub/kphp-json.stub.php index 5bb84bd2..eff82c0d 100644 --- a/src/test/fixtures/kphp_stub/kphp-json.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-json.stub.php @@ -2,3 +2,8 @@ DOC_COMMENT:PhpDocCommentStubImpl @kphp-json:@kphp-json CLASS:PhpClassStubImpl{C} + DOC_COMMENT:PhpDocCommentStubImpl + @kphp-json:@kphp-json + @kphp-json:@kphp-json + CLASS_FIELDS:PhpClassFieldListStubImpl + CLASS_FIELD:PhpFieldStubImpl diff --git a/src/test/fixtures/kphp_stub/kphp-serialize.fixture.php b/src/test/fixtures/kphp_stub/kphp-serialize.fixture.php new file mode 100644 index 00000000..af93c227 --- /dev/null +++ b/src/test/fixtures/kphp_stub/kphp-serialize.fixture.php @@ -0,0 +1,22 @@ + Date: Wed, 30 Jul 2025 17:21:15 +0300 Subject: [PATCH 6/6] backport new approach to walt a stub tree --- .../fixtures/kphp_stub/kphp-json.stub.php | 6 +-- .../kphp_stub/kphp-serialize.stub.php | 10 ++-- .../kphp_stub/kphp-simple-tags.stub.php | 10 ++-- .../fixtures/kphp_stub/kphp-template.stub.php | 2 +- .../kphp_stub/kphp-warn-performance.stub.php | 2 +- .../testing/infrastructure/StubTestBase.kt | 47 ++++++++++++++++++- 6 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/test/fixtures/kphp_stub/kphp-json.stub.php b/src/test/fixtures/kphp_stub/kphp-json.stub.php index eff82c0d..87a60052 100644 --- a/src/test/fixtures/kphp_stub/kphp-json.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-json.stub.php @@ -1,9 +1,9 @@ PhpFileStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-json:@kphp-json + @kphp-json {name: @kphp-json}:@kphp-json CLASS:PhpClassStubImpl{C} DOC_COMMENT:PhpDocCommentStubImpl - @kphp-json:@kphp-json - @kphp-json:@kphp-json + @kphp-json {name: @kphp-json}:@kphp-json + @kphp-json {name: @kphp-json}:@kphp-json CLASS_FIELDS:PhpClassFieldListStubImpl CLASS_FIELD:PhpFieldStubImpl diff --git a/src/test/fixtures/kphp_stub/kphp-serialize.stub.php b/src/test/fixtures/kphp_stub/kphp-serialize.stub.php index c8d51ddd..8296b4a1 100644 --- a/src/test/fixtures/kphp_stub/kphp-serialize.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-serialize.stub.php @@ -1,17 +1,17 @@ PhpFileStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-...:@kphp-serializable - @kphp-...:@kphp-reserved-fields + @kphp-... {name: @kphp-serializable}:@kphp-serializable + @kphp-... {name: @kphp-reserved-fields}:@kphp-reserved-fields CLASS:PhpClassStubImpl{C} DOC_COMMENT:PhpDocCommentStubImpl - @kphp-...:@kphp-serialized-float32 + @kphp-... {name: @kphp-serialized-float32}:@kphp-serialized-float32 CLASS_FIELDS:PhpClassFieldListStubImpl CLASS_FIELD:PhpFieldStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-...:@kphp-serialized-field + @kphp-... {name: @kphp-serialized-field}:@kphp-serialized-field CLASS_FIELDS:PhpClassFieldListStubImpl CLASS_FIELD:PhpFieldStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-...:@kphp-serialized-field + @kphp-... {name: @kphp-serialized-field}:@kphp-serialized-field CLASS_FIELDS:PhpClassFieldListStubImpl CLASS_FIELD:PhpFieldStubImpl diff --git a/src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php b/src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php index 221bcec2..13affd06 100644 --- a/src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php @@ -1,10 +1,10 @@ PhpFileStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-...:@kphp-throws - @kphp-...:@kphp-color - @kphp-...:@kphp-required - @kphp-...:@kphp-inline - @kphp-...:@kphp-profile + @kphp-... {name: @kphp-throws}:@kphp-throws + @kphp-... {name: @kphp-color}:@kphp-color + @kphp-... {name: @kphp-required}:@kphp-required + @kphp-... {name: @kphp-inline}:@kphp-inline + @kphp-... {name: @kphp-profile}:@kphp-profile FUNCTION:PhpFunctionStubImpl NOT_PROMOTED_PARAMETER:PhpParameterStubImpl NOT_PROMOTED_PARAMETER:PhpParameterStubImpl diff --git a/src/test/fixtures/kphp_stub/kphp-template.stub.php b/src/test/fixtures/kphp_stub/kphp-template.stub.php index 52bf0b40..afd06096 100644 --- a/src/test/fixtures/kphp_stub/kphp-template.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-template.stub.php @@ -1,4 +1,4 @@ PhpFileStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-template-class:@kphp-template-class: int,string + @kphp-template-class {name: @kphp-template-class, value: int,string}:@kphp-template-class: int,string CLASS:PhpClassStubImpl{C} diff --git a/src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php b/src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php index 10313d02..24b3092f 100644 --- a/src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php +++ b/src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php @@ -1,4 +1,4 @@ PhpFileStubImpl DOC_COMMENT:PhpDocCommentStubImpl - @kphp-warn-performance:@kphp-warn-performance + @kphp-warn-performance {name: @kphp-warn-performance}:@kphp-warn-performance FUNCTION:PhpFunctionStubImpl diff --git a/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt b/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt index 767e8856..31c01789 100644 --- a/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt +++ b/src/test/kotlin/com/vk/kphpstorm/testing/infrastructure/StubTestBase.kt @@ -1,6 +1,10 @@ package com.vk.kphpstorm.testing.infrastructure -import com.intellij.psi.impl.DebugUtil +import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.stubs.PsiFileStubImpl +import com.intellij.psi.stubs.Stub +import com.intellij.psi.stubs.StubElement +import com.jetbrains.php.lang.documentation.phpdoc.psi.stubs.PhpDocTagStub import com.jetbrains.php.lang.psi.stubs.PhpFileStubBuilder import com.vk.kphpstorm.configuration.KphpStormConfiguration import java.io.File @@ -19,7 +23,7 @@ abstract class StubTestBase : KphpStormTestBase() { myFixture.configureByFile(fixtureFile) val stubTree = stubBuilder.buildStubTree(myFixture.file) - val stubTreeString = DebugUtil.stubTreeToString(stubTree) + val stubTreeString = dumpToString(stubTree) val expectedFileRelPath = fixtureFile.replace(".fixture.php", ".stub.php") if (fixtureFile == expectedFileRelPath) { @@ -35,4 +39,43 @@ abstract class StubTestBase : KphpStormTestBase() { assertSameLinesWithFile(expectedFile.absolutePath, stubTreeString) } + + + private fun dumpToString(node: StubElement<*>): String { + val buffer = StringBuilder() + dumpToString(node, buffer, 0) + + return buffer.toString() + } + + private fun dumpToString(node: StubElement<*>, buffer: StringBuilder, indent: Int) { + StringUtil.repeatSymbol(buffer, ' ', indent) + + val presentable = getPresentable(node) + + if (presentable != null) { + buffer.append(presentable.toString()).append(':') + } + + buffer.append(node.toString()).append('\n') + + for (child in node.childrenStubs) { + dumpToString(child, buffer, indent + 2) + } + } + + @Suppress("UnstableApiUsage") + private fun getPresentable(node: Stub): Any? { + return when (node) { + is PsiFileStubImpl<*> -> { + null + } + + is PhpDocTagStub -> { + "${node.stubType} {name: ${node.name}${if (node.value != null) ", value: " + node.value else ""}}" + } + + else -> node.stubType + } + } }