Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Oct 4, 2014
0 parents commit 5076cd9
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
*.jar
44 changes: 44 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<idea-plugin version="2">
<id>cz.matej21.intellij.kdyby.doctrine</id>
<name>Kdyby Doctrine support</name>
<version>0.1.0</version>
<vendor email="[email protected]" url="http://www.matej21.cz">David Matejka</vendor>

<depends>com.intellij.modules.lang</depends>
<depends>com.jetbrains.php</depends>

<description><![CDATA[
Support for <strong>Kdyby Doctrine</strong> library
]]></description>

<change-notes><![CDATA[
<h2>0.1.0</h2>
<ul>
<li>First proof of concept</li>
</ul>
]]>
</change-notes>

<!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="133"/>

<application-components>
<!-- Add your application components here -->
</application-components>

<project-components>
<!-- Add your project components here -->
</project-components>

<actions>
<!-- Add your actions here -->
</actions>

<extensions defaultExtensionNs="com.intellij">
<php.typeProvider2 implementation="cz.matej21.intellij.kdyby.doctrine.RepositoryTypeProvider"/>
<php.typeProvider2 implementation="cz.matej21.intellij.kdyby.doctrine.RepositoryMethodTypeProvider"/>
<php.typeProvider2 implementation="cz.matej21.intellij.kdyby.doctrine.ForeachEntityTypeProvider"/>
</extensions>
</idea-plugin>
13 changes: 13 additions & 0 deletions intellij-kdyby-doctrine.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

63 changes: 63 additions & 0 deletions php-project/doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Kdyby\Doctrine;

class EntityManager
{
/**
* @param string
* @return EntityDao
*/
public function getDao($className)
{

}
}

class EntityDao
{
/**
* @return ResultSet
*/
public function fetch()
{

}

public function findAll()
{

}

/**
* @return array
*/
public function findBy()
{

}

/**
* @return object
*/
public function find()
{

}

/**
* @return object
*/
public function findOneBy()
{

}
}

class ResultSet
{
public function applyPaginator()
{

}

}
37 changes: 37 additions & 0 deletions php-project/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class Lorem
{
/** @var \Kdyby\Doctrine\EntityDao */
protected $fooDao;

public function __construct(\Kdyby\Doctrine\EntityManager $entityManager)
{
$this->fooDao = $entityManager->getDao(FooEntity::class);
}

/**
* @return FooEntity|object
*/
public function doSth()
{
return $this->fooDao->find();
}
}



/** @var \Kdyby\Doctrine\EntityManager $entityManager */
$entityDao = $entityManager->getDao(FooEntity::class);
$entityDao->find()->id;


foreach($entityDao->findAll() as $entity) {
$entity->id;
}

$result = $entityDao->fetch();
$result->applyPaginator();
foreach($result as $entity) {
$entity->id;
}
12 changes: 12 additions & 0 deletions php-project/model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
class FooEntity
{

public $id;

}

class BarEntity
{
public $foo;
}
79 changes: 79 additions & 0 deletions src/cz/matej21/intellij/kdyby/doctrine/ElementValueResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cz.matej21.intellij.kdyby.doctrine;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.*;

import java.util.ArrayList;
import java.util.Collection;


public class ElementValueResolver {

private final PsiElement element;

public ElementValueResolver(PsiElement element) {
super();
this.element = element;
}

public String resolve() {
try {
return doResolve(this.element);
} catch (UnresolvableValueException e) {
return null;
}
}

public static String resolve(PsiElement element) {
return (new ElementValueResolver(element)).resolve();
}


private String doResolve(PsiElement element) throws UnresolvableValueException {
if (element instanceof StringLiteralExpression) {
return ((StringLiteralExpression) element).getContents();
} else if (element instanceof BinaryExpression && element.getNode().getElementType().equals(PhpElementTypes.CONCATENATION_EXPRESSION)) {
BinaryExpression binaryExpression = (BinaryExpression) element;

return doResolve(binaryExpression.getLeftOperand()) + doResolve(binaryExpression.getRightOperand());
} else if (element instanceof ClassConstantReference) {
ClassConstantReference constantReference = (ClassConstantReference) element;
ClassReference classReference = (ClassReference) constantReference.getClassReference();
if (constantReference.getLastChild() instanceof LeafPsiElement) {
String constantName = constantReference.getLastChild().getText();
if (constantName.equals("class")) {
return classReference.getFQN();
}
for (PhpClass phpClass : getClasses(classReference, element.getProject())) {
Field constant = phpClass.findFieldByName(constantName, true);
if (constant != null && constant.isConstant()) {
try {
return doResolve(constant.getDefaultValue());
} catch (UnresolvableValueException e) {
}
}
}
}
}
throw new UnresolvableValueException();

}

private static Collection<PhpClass> getClasses(PhpTypedElement element, Project project) {
PhpIndex phpIndex = PhpIndex.getInstance(project);
Collection<PhpClass> classes = new ArrayList<PhpClass>();
for (String className : element.getType().getTypes()) {
classes.addAll(phpIndex.getClassesByFQN(className));
}

return classes;
}

private class UnresolvableValueException extends Exception {
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cz.matej21.intellij.kdyby.doctrine;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.ForeachStatement;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.elements.PhpTypedElement;
import com.jetbrains.php.lang.psi.elements.Variable;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider2;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeSignatureKey;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;


public class ForeachEntityTypeProvider implements PhpTypeProvider2 {

@Override
public char getKey() {
return '\u2102';
}

@Nullable
@Override
public String getType(PsiElement psiElement) {
if (!(psiElement instanceof Variable)) {
return null;
}
Variable variable = (Variable) psiElement;
PsiElement arrayAccessExpression = variable.getParent();
if (!(arrayAccessExpression instanceof ForeachStatement)) {
return null;
}
ForeachStatement arrayIndex = (ForeachStatement) arrayAccessExpression;
PsiElement operation = arrayIndex.getArray();
if (!(operation instanceof PhpTypedElement)) {
return null;
}
PhpType value = ((PhpTypedElement) operation).getType();
if (variable == arrayIndex.getKey()) {
return null;
}

if (variable != arrayIndex.getValue()) {
return null;
}
for (String strType : value.elementType().getTypes()) {
if (strType.length() < 2
|| strType.charAt(0) != '#'
|| strType.indexOf(RepositoryMethodTypeProvider.KEY) == -1
|| !strType.contains("[]")) {
continue;
}
if (PhpTypeSignatureKey.ARRAY_ELEMENT.is(strType.charAt(1))) {
strType = strType.substring(2);
}
return strType.substring(strType.indexOf(RepositoryMethodTypeProvider.KEY) + 1, strType.indexOf("[]"));
}
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) {
return PhpIndex.getInstance(project).getAnyByFQN(s);
}
}
Loading

0 comments on commit 5076cd9

Please sign in to comment.